pandas 更改多个DataFrame列的dtype的最简洁方法

lnlaulya  于 2023-05-12  发布在  其他
关注(0)|答案(3)|浏览(221)

我在Pandas(2.0.1)DataFrame中有四个类型为object的列,它们希望转换为int
应用以下方法:

cols = ['x1','x2','y1','y2']

df[cols] = df[cols].apply(pd.to_numeric)

# The same message is raised when trying to cast a single column:
df['x1'] = pd.to_numeric(df['x1'])

# The same message is also raised when using .astype():
dff[cols] = dff[cols].astype(int)

如本文所述:https://stackoverflow.com/a/28648923/6630397引发消息:

/tmp/ipykernel_87959/2834796204.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
    
See the caveats in the documentation:
https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df[cols] = df[cols].apply(pd.to_numeric)

如何正确地(快速地)将四列从object转换为int

puruo6ea

puruo6ea1#

可能的解决方案:

df[cols] = df[cols].astype('int')
u0sqgete

u0sqgete2#

通过使用.loc索引器来选择和修改原始 Dataframe 的特定列,可以避免SettingWithCopyWarning
我还将使用downcast参数将所需的输出数据类型指定为整数,因为如果列包含任何非整数值,则pd.to_numeric()可能返回float数据类型。

  • 密码 *
cols = ["x1", "x2", "y1", "y2"]

df.loc[:, cols] = df[cols].apply(pd.to_numeric, downcast="integer")
wdebmtf2

wdebmtf23#

我相信有时候pandas会有点过于急切地抛出警告,你的解决方案没有什么特别的问题,但也许这个会稍微干净一些:

df = df.astype({'x1': 'int', 'x2': 'int', 'y1': 'int', 'y2': 'int'})

相关问题