我有一个下面的Pandas数据框,在这里我想根据“时间”,“样本”和“ uid”列更改“ fmc”列的值。
概念如下:
对于相同的date
,if df.samples == 'C' & df.uid == 'Plot1'
则对应的行值fmc * 0.4
类似地,对于相同的date
,if df.samples == 'C' and df.uid == 'Plot2'
则对应的的行值fmc*0.8
对于相同的date
,if df.samples == 'E' & df.uid == 'Plot1'
则对应的行值fmc * 0.4
类似地,对于相同的date
,if df.samples == 'E' and df.uid == 'Plot2'
则对应的的行值fmc*0.15
对于相同的date
,if df.samples == 'ns' & df.uid == 'Plot1'
则对应的行值fmc * 0.2
类似地,对于相同的date
,if df.samples == 'ns' and df.uid == 'Plot2'
则对应的的行值fmc*0.05
我是python的新手,所以如果无法解释清楚,我深表歉意,如果您需要更多说明,请告诉我。
time samples uid fmc
0 2015-10-11 C Plot1 98.226352
1 2015-10-11 C Plot2 132.984817
2 2015-10-11 E Plot1 114.147964
3 2015-10-11 E Plot2 110.083699
4 2015-10-11 ns Plot1 113.258977
5 2015-10-11 ns Plot2 113.768023
6 2015-10-19 C Plot1 118.503214
7 2015-10-19 E Plot1 108.733209
8 2015-10-19 ns Plot1 59.316977
9 2015-10-27 C Plot1 104.977531
10 2015-10-27 C Plot2 121.213887
11 2015-10-27 E Plot1 129.575670
12 2015-10-27 E Plot2 118.639048
13 2015-10-27 ns Plot1 103.581065
14 2015-10-27 ns Plot2 102.278469
15 2015-11-17 C Plot1 103.820689
16 2015-11-17 C Plot2 117.333382
17 2015-11-17 E Plot1 143.418932
18 2015-11-17 E Plot2 160.342155
19 2015-11-17 ns Plot1 89.890484
这段代码:
import pandas as pd
data = [
['2015-10-11', 'C', 'Plot1', 98.226352 ],
['2015-10-11', 'C', 'Plot2', 132.984817 ],
['2015-10-11', 'E', 'Plot1', 114.147964 ],
['2015-10-11', 'E', 'Plot2', 110.083699 ],
['2015-10-11', 'ns', 'Plot1', 113.258977 ],
['2015-10-11', 'ns', 'Plot2', 113.768023 ],
['2015-10-19', 'C', 'Plot1', 118.503214 ],
['2015-10-19', 'E', 'Plot1', 108.733209 ],
['2015-10-19', 'ns', 'Plot1', 59.316977 ],
['2015-10-27', 'C', 'Plot1', 104.977531 ],
['2015-10-27', 'C', 'Plot2', 121.213887 ],
['2015-10-27', 'E', 'Plot1', 129.575670 ],
['2015-10-27', 'E', 'Plot2', 118.639048 ],
['2015-10-27', 'ns', 'Plot1', 103.581065 ],
['2015-10-27', 'ns', 'Plot2', 102.278469 ],
['2015-11-17', 'C', 'Plot1', 103.820689 ],
['2015-11-17', 'C', 'Plot2', 117.333382 ],
['2015-11-17', 'E', 'Plot1', 143.418932 ],
['2015-11-17', 'E', 'Plot2', 160.342155 ],
['2015-11-17', 'ns', 'Plot1', 89.890484]
]
df = pd.DataFrame(columns=['time', 'samples', 'uid', 'fmc'], data=data)
print (df.head(10))
df['result'] = df.apply(
lambda item:
(item.fmc * 0.4) if item.samples == 'C' and item.uid == 'Plot1' else \
(item.fmc * 0.8) if item.samples == 'C' and item.uid == 'Plot2' else \
(item.fmc * 0.4) if item.samples == 'E' and item.uid == 'Plot1' else \
(item.fmc * 0.15)if item.samples == 'E' and item.uid == 'Plot2' else \
(item.fmc * 0.2) if item.samples == 'ns'and item.uid == 'Plot1' else \
(item.fmc * 0.05)if item.samples == 'ns'and item.uid == 'Plot2' else None,
axis=1
)
print(df.head(10))
应产生以下输出:
time samples uid fmc
0 2015-10-11 C Plot1 98.226352
1 2015-10-11 C Plot2 132.984817
2 2015-10-11 E Plot1 114.147964
3 2015-10-11 E Plot2 110.083699
4 2015-10-11 ns Plot1 113.258977
5 2015-10-11 ns Plot2 113.768023
6 2015-10-19 C Plot1 118.503214
7 2015-10-19 E Plot1 108.733209
8 2015-10-19 ns Plot1 59.316977
9 2015-10-27 C Plot1 104.977531
time samples uid fmc result
0 2015-10-11 C Plot1 98.226352 39.290541
1 2015-10-11 C Plot2 132.984817 106.387854
2 2015-10-11 E Plot1 114.147964 45.659186
3 2015-10-11 E Plot2 110.083699 16.512555
4 2015-10-11 ns Plot1 113.258977 22.651795
5 2015-10-11 ns Plot2 113.768023 5.688401
6 2015-10-19 C Plot1 118.503214 47.401286
7 2015-10-19 E Plot1 108.733209 43.493284
8 2015-10-19 ns Plot1 59.316977 11.863395
9 2015-10-27 C Plot1 104.977531 41.991012
Process finished with exit code 0
受df.apply
,使用axis=1
和启发并传递包含全套条件的lambda函数的启发,您将在result
列中获得期望的值。
该apply
函数会将数据框的列(由于axis=1
)传递给lambda函数,就像item
一系列值中的每个记录一样。lambda函数还会result
为系列中的每个给定记录/项目返回相应的值,因此我们不必担心日期/索引值的匹配。
参考pandas.DataFrame.apply
这里。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句