[英]How to modify DataFrame column without getting SettingWithCopyWarning?
I have a DataFrame
object df
.我有一个DataFrame
对象df
。 And I would like to modify job
column so that all retired people are 1 and rest 0 (like shown here ):我想修改job
列,使所有退休人员为1和0休息(如显示这里):
df['job'] = df['job'].apply(lambda x: 1 if x == "retired" else 0)
But I get a warning:但我收到警告:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
Why did I get it here though?可为什么我会在这里得到它? From what I read it applies to situations where I take a slice of rows and then a column, but here I am just modyfing elements in a row.从我读到的内容来看,它适用于我先取一行然后取一列的情况,但在这里我只是修改一行中的元素。 Is there a better way to do that?有没有更好的方法来做到这一点?
Use:用:
df['job']=df['job'].eq('retired').astype(int)
or或者
df['job']=np.where(df['job'].eq('retired'),1,0)
So here's an example dataframe:所以这是一个示例数据框:
import pandas as pd
import numpy as np
data = {'job':['retired', 'a', 'b', 'retired']}
df = pd.DataFrame(data)
print(df)
job
0 retired
1 a
2 b
3 retired
Now, you can make use of numpy's where
function:现在,您可以使用 numpy 的where
函数:
df['job'] = np.where(df['job']=='retired', 1, 0)
print(df)
job
0 1
1 0
2 0
3 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.