簡體   English   中英

如何根據條件字典重新計算 DataFrame 列值(Pandas Python)

[英]How to recalculate DataFrame column values based on condition dict (Pandas Python)

假設我有以下數據幀:

一種
0 aa 4.32
1 aa 7.00
2 bb 8.00
3 74.00
4 抄送 30.00
5 bb 2.00

假設我有以下字典,它確定其鍵中 A 列的條件並確定其值中 coulmn B 的乘數:

dict1={'aa':-1, 'bb':2}

我想要的只是基於 A 列中的值對 dict1 鍵是 queal 的條件,將 B 列中的值與 dict1 中的值相乘。

所以輸出應該是:

一種
0 aa -4.32
1 aa -7.00
2 bb 16.00
3 74.00
4 抄送 30.00
5 bb 4.00

謝謝

使用pd.Series.map

print (df["A"].map(dict1).fillna(1)*df["B"])

0    -4.32
1    -7.00
2    16.00
3    74.00
4    30.00
5     4.00
dtype: float64

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM