简体   繁体   English

基于多列条件替换 Pandas 中的 NaN

[英]Replace NaN in Pandas based on a multi-column condition

I hope you are doing well.我希望你做得很好。

I need help to perform a complex "NaN replace" on my dataframe.我需要帮助在我的数据帧上执行复杂的“NaN 替换”。

What is the best way to replace NaN values in a pandas column, based on a mode of other column values filtered by other columns?根据其他列过滤的其他列值的模式,替换 Pandas 列中的 NaN 值的最佳方法是什么?

Let me illustrate my problem:让我来说明我的问题:

import random
import numpy as np
import pandas as pd
data = {'Region': [1,1,1,2,2,2,1,2,2,2,2,1,1,1,2,1], 'Country': ['a','a', 'a', 'a', 'a','a', 'a', 'a', 'b', 'b', 'b', 'b','b','b','b','b'], 'GDP' : [100,100,101,105,105,110,np.nan,np.nan,200,200,100,150,100,150,np.nan,np.nan]}
df = pd.DataFrame.from_dict(data)

df: df:

     Region Country GDP
0        1       a  100.0
1        1       a  100.0
2        1       a  101.0
3        2       a  105.0
4        2       a  105.0
5        2       a  110.0
6        1       a    NaN
7        2       a    NaN
8        2       b  200.0
9        2       b  200.0
10       2       b  100.0
11       1       b  150.0
12       1       b  100.0
13       1       b  150.0
14       2       b    NaN
15       1       b    NaN

I would like to replace the nan values of the GDP column with the mode of other GDP values for the same country and region.我想将GDP列的nan值替换为同一国家和地区的其他GDP值的众数。

In the case of the NaN value of the GDP column of index 6, I wish to replace it with 100 (as it is the mode for GDP values for Region 1 & Country a)对于索引 6 的 GDP 列的 NaN 值,我希望将其替换为 100(因为它是区域 1 和国家 a 的 GDP 值的模式)

The desired output should look like this:所需的输出应如下所示:

    Region Country  GDP
0        1       a  100
1        1       a  100
2        1       a  101
3        2       a  105
4        2       a  105
5        2       a  110
6        1       a  100
7        2       a  105
8        2       b  200
9        2       b  200
10       2       b  100
11       1       b  150
12       1       b  100
13       1       b  150
14       2       b  200
15       1       b  150 

Thank you for your help, I hope you have an excellent day!感谢您的帮助,希望您有美好的一天!

Pandas' fillna allows for filling missing values from another series. Pandas 的fillna允许填充另一个系列的缺失值。 So we need another series that contains the mode of each Country/Region at the corresponding indices.因此,我们需要另一个系列,其中包含每个国家/地区在相应索引处的模式。

To get this series, we can use Pandas' groupby().transform() operation.为了得到这个系列,我们可以使用 Pandas 的groupby().transform()操作。 It groups the dataframe, and then broadcasts the results back to the original shape.它将数据帧分组,然后将结果广播回原始形状。

If we use this operation with mode as is, it will give an error.如果我们用这种操作与mode的是,它会给出一个错误。 Mode can return multiple values, preventing pandas from broadcasting the values back to the original shape.模式可以返回多个值,防止熊猫将值广播回原始形状。 So we need to force it to return a single value, so just pick the first one (or last one, or whichever).所以我们需要强制它返回一个值,所以只需选择第一个(或最后一个,或以任何一个)。

df["GDP"].fillna(
    df.groupby(["Country", "Region"])["GDP"].transform(
        lambda x: x.mode()[0]
    )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM