此代码的执行有效:
import pandas as pd
df = pd.DataFrame({'name':["Adam", "Sarah", "Tom", "Sarah", "Adam", "Tom", "Will"], 'score':[1,16,2,32,11,9,50]})
print(df)
colName = 'score'
df[colName][df[colName] <= 10] = 1
df[colName][(df[colName] > 10) & (df[colName] <= 20)] = 11
df[colName][df[colName] > 20] = 21
print(df)
...但是抛出此警告:
test.py:9:SettingWithCopyWarning:正在尝试在 来自DataFrame的切片的副本
请参阅文档中的警告: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df [colName] [df [colName] <= 10] = 1 test.py:10:SettingWithCopyWarning: 试图在DataFrame的切片副本上设置一个值
请参阅文档中的警告: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df [colName] [(df [colName]> 10)&(df [colName] <= 20)] = 11 test.py:11: SettingWithCopyWarning:试图在一个副本上设置一个值 从DataFrame切片
请参阅文档中的警告: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df [colName] [df [colName]> 20] = 21
我猜这是围绕深层/浅层复制的问题吗?但是我该如何解决?必须有一种简单易读的方式来进行这样的简单操作吗?
编辑: 它适用于:
df.loc[df[colName] <= 10, colName] = 1
...但是这很不合逻辑,因为colName作为第二个参数是违反直觉的...
答案 0 :(得分:0)
尝试以下代码,希望对您有所帮助。
import pandas as pd
import numpy as np
df = pd.DataFrame({'name':["Adam", "Sarah", "Tom", "Sarah", "Adam", "Tom", "Will"], 'score':[1,16,2,32,11,9,50]})
print(df)
colName = 'score'
df[colName] = np.where(df[colName] <= 10, 1, df[colName])
df[colName] = np.where((df[colName] > 10) & (df[colName] <= 20), 11 , df[colName])
df[colName] = np.where(df[colName] > 20, 21 , df[colName])
print(df)
输出将会是:
name score
0 Adam 1
1 Sarah 16
2 Tom 2
3 Sarah 32
4 Adam 11
5 Tom 9
6 Will 50
****NEW*********
name score
0 Adam 1
1 Sarah 11
2 Tom 1
3 Sarah 21
4 Adam 11
5 Tom 1
6 Will 21
这不会发出任何警告,因为您不在使用数据框的任何部分,而是使用条件子句并更新列的值。