5
我按照建議here來更改熊貓數據框的列數據類型。但是,如果我通過索引號而不是列名來引用列,它似乎不起作用。有沒有辦法做到這一點?熊貓:無法更改列數據類型
In [49]: df.iloc[:, 4:].astype(int)
Out[49]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5074 entries, 0 to 5073
Data columns (total 3 columns):
5 5074 non-null values
6 5074 non-null values
7 5074 non-null values
dtypes: int64(3)
In [50]: df.iloc[:, 4:] = df.iloc[:, 4:].astype(int)
In [51]: df
Out[51]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5074 entries, 0 to 5073
Data columns (total 7 columns):
1 5074 non-null values
2 5074 non-null values
3 5074 non-null values
4 5074 non-null values
5 5074 non-null values
6 5074 non-null values
7 5074 non-null values
dtypes: object(7)
In [52]: