我不知道如何将一个简单的函数应用到Panda数据框中的每一行。在
示例:
def delLastThree(x):
x = x.strip()
x = x[:-3]
return x
arr = ['test123','test234','test453']
arrDF = pandas.DataFrame(arr)
arrDF.columns = ['colOne']
arrDF['colOne'].apply(delLastThree)
print arrDF
我希望下面的代码对每一行都返回'test'。相反,它会打印原始值。在
如何将delLastThree函数应用于DF中的每一行?在
当选择使用带
df['colOne']
的单括号时,您正在创建pd.Series
。在在
DataFrame
上使用.apply(func, axis=1)
,即使用[['colOne']]
进行选择,或者不选择任何列。然而,如果你需要修改cd6},那么你需要使用cd6}。在对于使用}。在
['colOne']
选择得到的pd.Series
,您可以使用.apply()
或{现在使用任意一个
^{pr2}$或者
获得:
当然,你也可以:
对序列使用map()函数(单列):
或者如果你想改变它:
^{pr2}$但正如@Stefan所说,这将是更快、更高效、更“泛文化”的:
或者,如果要删除所有尾随空格和数字:
测试:
相关问题 更多 >
编程相关推荐