3
我有一個數據幀(DF)與以下值:Python的熊貓加列值基於條件
Title
fintech_countries
US 60
UK 54
India 28
Australia 25
Germany 13
Singapore 11
Canada 10
我想所有國家增加其值< 25,並顯示他們與「其他」的總和(34)。
我已經通過下面的代碼創建一個列名的國家:
df1 = df.rename_axis('fintech_countries').rename_axis("countries", axis="columns" , inplace=True)
countries Title
fintech_countries
US 60
UK 54
India 28
Australia 25
Germany 13
Singapore 11
Canada 10
現在,我已經嘗試基於另一個查詢StackOverflow上下面的代碼:
df1.loc[df1['Title'] < 25, "countries"].sum()
,但我得到的以下錯誤:
KeyError: 'the label [countries] is not in the [columns]'
有人可以幫忙嗎?我需要的最終輸出爲:
countries Title
fintech_countries
US 60
UK 54
India 28
Australia 25
Others 34
TIA
由於一噸!有效 – chhibbz