Reputation: 195
df=pd.read_excel('Canada.xlsx',sheet_name='Canada by Citizenship',skiprows=range(20),skipfooter=2)
df['Total']=df.iloc[:,'1980':'2013'].sum(axis=1)
TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [1980] of <class 'str'>
I received the dataset from this link
Upvotes: 1
Views: 320
Reputation: 3331
The iloc()
function requires only positional arguments. You could use the loc()
function to do it in on line.
Here is what it could look like :
df.loc[:, [1980 + i for i in range(34)] ].sum(axis=1)
Upvotes: 0
Reputation: 59549
The columns are integers. You can slice with:
df.loc[:, range(1980, 2014)].sum(1)
# or
df.iloc[:, df.columns.get_loc(1980):df.columns.get_loc(2013)+1].sum(1)
0 58639
1 15699
2 69439
3 6
4 15
...
190 97146
191 2
192 2985
193 1677
194 8598
Length: 195, dtype: int64
Upvotes: 3