【发布时间】:2019-04-08 02:35:03
【问题描述】:
我有一个带有 US Congress biographical data 的 .csv,我将其读取为 Panda df:
df = pd.read_csv('congress100.csv', delimiter = ';', names = ['Name', 'Position', 'Party', 'State', 'Congress'], header = 0)
我的数据框如下所示:
0 'ACKERMAN, Gary Leonard' 'Representative' 'Democrat' 'NY' '100(1987-1988)'
1 'ADAMS, Brockman (Brock)' 'Senator' 'Democrat' 'WA' '100(1987-1988)'
2 'AKAKA, Daniel Kahikina' 'Representative' 'Democrat' 'HI' '100(1987-1988)'
3 'ALEXANDER, William Vollie (Bill), Jr.' 'Representative' 'Democrat' 'AR' '100(1987-1988)'
4 'ANDERSON, Glenn Malcolm' 'Representative' 'Democrat' 'CA' '100(1987-1988)'
5 'ANDREWS, Michael Allen' 'Representative' 'Democrat' 'TX' '100(1987-1988)'
6 'ANNUNZIO, Frank' 'Representative' 'Democrat' 'IL' '100(1987-1988)'
7 'ANTHONY, Beryl Franklin, Jr.' 'Representative' 'Democrat' 'AR' '100(1987-1988)'
8 'APPLEGATE, Douglas Earl' 'Representative' 'Democrat' 'OH' '100(1987-1988)'
9 'ARCHER, William Reynolds, Jr.' 'Representative' 'Republican' 'TX' '100(1987-1988)'
10 'ARMEY, Richard Keith' 'Representative' 'Republican' 'TX' '100(1987-1988)'
我想将“Congress”列中的数据转换为整数。现在,我首先将其转换为更简单的字符串:
df['Congress'] = df['Congress'].str.replace(r'100\(1987-1988\)', '1987')
这是成功的。但是,我正在尝试将那个更简单的字符串转换为整数:
df['Congress'] = df['Congress'].pd.to_numeric(errors='ignore')
我收到一个错误:
AttributeError: 'Series' object has no attribute 'pd'
请帮我解决这个错误并简化我的代码。
【问题讨论】: