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屋!
查看全文