有几个浮点列,我想将所有的浮点列转换为int。
在知道那些浮点列的列名之前,如何将它们全部转换为int类型?
答案 0 :(得分:1)
您可以为此使用一个简单的for循环:
# say df is the dataframe
for c in df.columns:
if df[c].dtype == np.float:
df[c] = df[c].astype(int)
答案 1 :(得分:1)
您可以先select_dtypes
,然后round
,最后使用支持可空Int dtype的Int64
转换为df.astype
:
m = df.select_dtypes(np.number)
df[m.columns]= m.round().astype('Int64')