像这样的数据框,我正在添加映射和计算中的一些列。
code month of entry name reports
0 JJ 20171002 Jason 14
1 MM 20171206 Molly 24
2 TT 20171208 Tina 31
3 JJ 20171018 Jake 22
4 AA 20090506 Amy 34
5 DD 20171128 Daisy 16
6 RR 20101216 River 47
7 KK 20171230 Kate 32
8 DD 20171115 David 14
9 JJ 20171030 Jack 10
10 NN 20171216 Nancy 28
它在这里做的是选择一些行并从字典中查找值并从简单计算中插入另一列。它工作正常:
import pandas as pd
data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy', 'Daisy', 'River', 'Kate', 'David', 'Jack', 'Nancy'],
'code' : ['JJ', 'MM', 'TT', 'JJ', 'AA', 'DD', 'RR', 'KK', 'DD', 'JJ', 'NN'],
'month of entry': ["20171002", "20171206", "20171208", "20171018", "20090506", "20171128", "20101216", "20171230", "20171115", "20171030", "20171216"],
'reports': [14, 24, 31, 22, 34, 16, 47, 32, 14, 10, 28]}
df = pd.DataFrame(data)
dict_hour = {'JasonJJ' : 3, 'MollyMM' : 6, 'TinaTT' : 2, 'JakeJJ' : 3, 'AmyAA' : 8, 'DaisyDD' : 6, 'RiverRR' : 4, 'KateKK' : 8, 'DavidDD' : 5, 'JackJJ' : 5, 'NancyNN' : 2}
wanted = ['JasonJJ', 'TinaTT', 'AmyAA', 'DaisyDD', 'KateKK']
df['name_code'] = df['name'].astype(str) + df['code'].astype(str)
df1 = df[df['name_code'].isin(wanted)]
df1['hour'] = df1['name_code'].map(dict_hour).astype(float)
df1['coefficient'] = df1['reports'] / df1['hour'] - 1
但是最后两行收到了同样的警告:
SettingWithCopyWarning:
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
如何相应地改进代码?谢谢。
答案 0 :(得分:3)
您需要copy
:
df1 = df[df['name_code'].isin(wanted)].copy()
如果稍后修改df1
中的值,您会发现修改不会传播回原始数据(df
),并且Pandas会发出警告。