[英]How can I change the value of the last row of a dataframe if some conditions for that last row is satisfied?
[英]Can I replace the null value of a cell in a dataframe if two conditions are satisfied?
我們到了!
大家好,我正在嘗試編寫一個循環,其中:如果特定列中單元格的值為“無”並且下一個單元格的值等於國家/地區數組,那么我將“無”值更改為下一個單元格的值,即國家名稱。
我想我在解決這個問題上有點偏離。
country_with_province = array(['Australia', 'China', 'France'], dtype=object)
for m in country_with_province:
if df5.loc[(df5['Province/State'] == None) and (df5['Country/Region'] == m):
df5.loc[:, "Province"] = df5.Province.fillna(m)
輸入:
日期 | 省/州 | 國家/地區 | 恢復 | 省 | |
---|---|---|---|---|---|
0 | 22/01/2020 | 沒有任何 | 阿富汗 | 0 | 沒有任何 |
1 | 22/01/2020 | 沒有任何 | 阿爾巴尼亞 | 0 | 沒有任何 |
2 | 22/01/2020 | 塔斯馬尼亞 | 澳大利亞 | 0 | 塔斯馬尼亞 |
3 | 22/01/2020 | 沒有任何 | 澳大利亞 | 0 | 沒有任何 |
4 | 22/01/2020 | 河南 | 中國 | 0 | 河南 |
5 | 22/01/2020 | 甘肅 | 中國 | 0 | 甘肅 |
6 | 22/01/2020 | 沒有任何 | 中國 | 0 | 沒有任何 |
7 | 22/01/2020 | 幾內亞法語 | 法國 | 0 | 幾內亞法語 |
8 | 22/01/2020 | 沒有任何 | 法國 | 0 | 沒有任何 |
OUTPUT:
日期 | 省/州 | 國家/地區 | 恢復 | 省 | |
---|---|---|---|---|---|
0 | 22/01/2020 | 沒有任何 | 阿富汗 | 0 | 沒有任何 |
1 | 22/01/2020 | 沒有任何 | 阿爾巴尼亞 | 0 | 沒有任何 |
2 | 22/01/2020 | 塔斯馬尼亞 | 澳大利亞 | 0 | 塔斯馬尼亞 |
3 | 22/01/2020 | 沒有任何 | 澳大利亞 | 0 | 澳大利亞 |
4 | 22/01/2020 | 河南 | 中國 | 0 | 河南 |
5 | 22/01/2020 | 甘肅 | 中國 | 0 | 甘肅 |
6 | 22/01/2020 | 沒有任何 | 中國 | 0 | 中國 |
7 | 22/01/2020 | 幾內亞法語 | 法國 | 0 | 幾內亞法語 |
8 | 22/01/2020 | 沒有任何 | 法國 | 0 | 法國 |
我正在處理 87552 行的 aa df。
Ty伙計們,我希望你能幫助我,我試圖在堆棧上搜索,但我失敗了!
看起來你的方法效果很好,所以......但你想要更好的東西
sel = df5['Province/State'].isnull() & df5['Country/Region'].isin(country_with_province)
df5.loc[sel, "Province"] = df5.loc[sel, 'Country/Region']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.