如何使用Apply()和自定义函数来更改DataFrame中的数据?

时间:2017-09-01 14:10:07

标签: python pandas

在DataFrame中不同行的索引列中进行某些更改的最简单方法是什么?

def fn(country):
    if any(char.isdigit() for char in country):
        return country[:-2]
    else:
        return country 

df.loc["Country"].apply(fn,axis=1)

1 个答案:

答案 0 :(得分:0)

我现在不能测试。您可以尝试:df['Country'] = df.apply(lambda row: fn(row),axis = 1)并更改您的函数参数以将该行考虑在内(如row['Country'])。这样,您可以使用其他列值逐行操作您想要的任何内容。