[英]Pandas: Combine two data-frames with different shape based on one common column
[英]Adding two data-frames with different values in a common column
我有兩個數據框。 我需要添加它們來計算比賽的總數並根據答案執行進一步的計算。 但是每次我嘗試添加它們時,某些行都有 Nan 值。
添加它們的結果是這樣的
如何在不獲取 NaN 值的情況下添加它們???
加入並按團隊分組后的總和:
ipl=pd.concat([ipl17,ipl18]).groupby('Team').sum().reset_index()
print(ipl)
輸出:
Team Matches Won Lost Tied N/R Points NRR For Against
0 CSK 14 9 5 0 0 18 0.253 2488 2433
1 DD 28 11 17 0 0 22 -0.734 4516 4559
2 GL 14 4 10 0 0 8 -0.412 2406 2472
3 KKR 28 16 12 0 0 32 0.571 4692 4725
4 KXIP 28 13 15 0 0 26 -0.379 4417 4488
5 MI 28 16 12 0 0 32 1.101 4787 4524
6 RCB 28 9 18 0 1 19 -1.170 4167 4416
7 RPS 14 9 5 0 0 18 0.176 2180 2165
8 RR 14 7 7 0 0 14 -0.250 2130 2141
9 SRH 28 17 10 0 1 35 0.753 4451 4311
解釋:
使用concat
連接兩個dataframe
。 使用groupby('name')
按Team
分組。 隨后,獲得每個團隊的總和。 然后使用reset_index
將index
(Team) 轉換為columns
。 如果你喜歡后者,你可以跳過它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.