dra*_*ine 5 python python-2.7 pandas
我目前迷失在熊猫文档的深处。我的问题是这样的:
我有一个简单的数据框
col1 col2
1 A
4 B
5 X
Run Code Online (Sandbox Code Playgroud)
我的目标是应用以下内容:
df['col1'] = df['col1'].apply(square)
Run Code Online (Sandbox Code Playgroud)
其中 square 是一个明确定义的函数。但是这个操作会抛出错误警告(并产生不正确的结果)
试图在来自 DataFrame 的切片副本上设置值。尝试使用 .loc[row_indexer,col_indexer] = value 代替
我无法理解这一点,也无法理解它指向的文档。我的工作流程是线性的(以防这使得更广泛的解决方案可行)。
熊猫 0.17.1 和 Python 2.7
非常感谢所有帮助。
它对我来说工作正常(pandas 0.18.1):
In [31]: def square(x):
....: return x ** 2
....:
In [33]: df
Out[33]:
col1 col2
0 1 A
1 4 B
2 5 X
In [35]: df.col1 = df.col1.apply(square)
In [36]: df
Out[36]:
col1 col2
0 1 A
1 16 B
2 25 X
Run Code Online (Sandbox Code Playgroud)
PS它也可能取决于你的功能的实现......