簡體   English   中英

使用 Panda 使用來自另一行和另一列的數據創建新列

[英]Using Panda to create new column with data from another row and column

我正在嘗試使用當前行“已確認”-昨天“已確認”為“新確診病例”創建一個新行。 確認是累積的。

我的數據如圖

Country,Date,Confirmed,Deaths,Recovered,Active
China,2020-01-21,10,5,1,100
China,2020-01-22,20,10,2,104
China,2020-01-23,30,15,3,116 
France,2020-01-21,20,5,1,100
France,2020-01-22,30,10,2,118
France,2020-01-23,40,15,3,138


Output 通緝

Country,Date,Confirmed,Deaths,Recovered,Active,New Confirmed
China,2020-01-21,10,5,1,100,0
China,2020-01-22,20,10,2,104,10
China,2020-01-23,30,15,3,116,10
France,2020-01-21,20,5,1,100,0
France,2020-01-22,30,10,2,118,10
France,2020-01-23,40,15,3,138,10

如果使用同一行的數據,我知道如何添加新行,但不確定如何使用另一行的數據。 任何提示或建議將不勝感激。

您可以使用shift()方法、 fillna()方法和astype()方法:

df['New Confirmed']=df['Confirmed']-df['Confirmed'].shift(1).fillna(df['Confirmed']).astype(int)

現在,如果您打印df ,您將獲得所需的 output:

    Country     Date    Confirmed   Deaths  Recovered   Active  New Confirmed
0   China   2020-01-21  10          5       1           100     0
1   China   2020-01-22  20          10      2           104     10
2   China   2020-01-23  30          15      3           116     10

更新:

對於這種情況,使用groupby()方法、 shift()方法、 fillna()方法和astype()方法:

df['New Confirmed']=df['Confirmed']-df.groupby('Country')['Confirmed'].shift(1).fillna(df['Confirmed']).astype(int)

上述代碼的Output:

    Country     Date    Confirmed   Deaths  Recovered   Active  New Confirmed
0   China   2020-01-21  10          5       1           100     0
1   China   2020-01-22  20          10      2           104     10
2   China   2020-01-23  30          15      3           116     10
3   France  2020-01-21  10          5       1           100     0
4   France  2020-01-22  20          10      2           104     10
5   France  2020-01-23  30          15      3           116     10

暫無
暫無

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

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