本文介绍了Python,Pandas从计算中添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
像这样的数据框,我从映射和计算中添加了一些列。
A data frame like this and I am adding some columns from mapping and calculation.
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
这里要做的是选择一些行并从字典中查找值,然后从中插入另一列简单的计算。效果很好:
What it is doing here is select some rows and look up the values from the dictionary and insert a further column from simple calculation. It works fine:
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
最后2行收到相同的警告:
But the last 2 lines received a same warning:
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
代码如何得到相应的改善?谢谢。
How can the code can be improved accordingly? Thank you.
推荐答案
您需要:
df1 = df[df['name_code'].isin(wanted)].copy()
如果以后修改 df1
中的值,您会发现修改不会传播回原始数据( df
),而熊猫会警告。
If you modify values in df1
later you will find that the modifications do not propagate back to the original data (df
), and that Pandas does warning.
这篇关于Python,Pandas从计算中添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!