使用 pandas 将 csv 文件中的特定列更改为整数
Change specific columns in a csv file into integer with pandas
我有一个包含 6 列的 csv 文件,格式如下:
2021-04-13 11:03:13+02:00,3.0,3.0,3.0,12.0,12.0
现在我想去掉除第一列以外的每一列的小数点。
我已经尝试过使用 df.style.format
以及 df.astype(int)
和 pd.set_option('precision', 0)
但在后两者中它总是卡在第一列,因为这不太合适( ValueError: invalid literal for int() with base 10: '2021-04-13 11:03:13+02:00'
)
如有任何帮助,我们将不胜感激!
使用您显示的示例,请尝试执行以下操作。使用 pandas.
的 iloc
功能
df.iloc[:,1:] = df.iloc[:,1:].astype(int)
df在运行上面的代码后会是这样的:
0,2021-04-13 11:03:13+02:00,3,3,3,12,12
我有一个包含 6 列的 csv 文件,格式如下:
2021-04-13 11:03:13+02:00,3.0,3.0,3.0,12.0,12.0
现在我想去掉除第一列以外的每一列的小数点。
我已经尝试过使用 df.style.format
以及 df.astype(int)
和 pd.set_option('precision', 0)
但在后两者中它总是卡在第一列,因为这不太合适( ValueError: invalid literal for int() with base 10: '2021-04-13 11:03:13+02:00'
)
如有任何帮助,我们将不胜感激!
使用您显示的示例,请尝试执行以下操作。使用 pandas.
的iloc
功能
df.iloc[:,1:] = df.iloc[:,1:].astype(int)
df在运行上面的代码后会是这样的:
0,2021-04-13 11:03:13+02:00,3,3,3,12,12