Pandas df.apply 不修改 DataFrame [英] Pandas df.apply does not modify DataFrame
本文介绍了Pandas df.apply 不修改 DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚开始使用熊猫,所以如果这是愚蠢的,请原谅.
I am just starting pandas so please forgive if this is something stupid.
我正在尝试将一个函数应用于一列,但它不起作用,我也没有看到任何错误.
I am trying to apply a function to a column but its not working and i don't see any errors also.
capitalizer = lambda x: x.upper()
for df in pd.read_csv(downloaded_file, chunksize=2, compression='gzip', low_memory=False):
df['level1'].apply(capitalizer)
print df
exit(1)
此打印显示 level1
列值与原始 csv 相同,而不是 upper
.我在这里遗漏了什么吗?
This print shows the level1
column values same as the original csv not doing upper
. Am i missing something here ?
谢谢
推荐答案
apply
不是 inplace 函数 - 它不会修改原始对象中的值,因此您需要重新分配:
apply
is not an inplace function - it does not modify values in the original object, so you need to assign it back:
df['level1'] = df['level1'].apply(capitalizer)
<小时>
或者,您可以使用 str.upper
,它应该会快得多.
df['level1'] = df['level1'].str.upper()
这篇关于Pandas df.apply 不修改 DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文