多列的Pandas图,每列有一个模式

4sup72z8  于 2022-11-05  发布在  其他
关注(0)|答案(5)|浏览(345)

在处理人口普查数据时,我想将两列(“workclass”和“native-country”)中的NaN替换为这两列各自的模式。我可以很容易地获得模式:

mode = df.filter(["workclass", "native-country"]).mode()

其返回 Dataframe :

workclass native-country
0   Private  United-States

然而,在一些情况下,

df.filter(["workclass", "native-country"]).fillna(mode)
    • 不**用任何东西替换每一列中的NaN,更不用说对应于该列的模式了。有没有一种简单的方法可以做到这一点?
wa7juj8i

wa7juj8i1#

如果要在 Dataframe df的某些列中使用mode插补缺失值,您只需通过按iloc的位置选择创建fillna by Series

cols = ["workclass", "native-country"]
df[cols]=df[cols].fillna(df.mode().iloc[0])

或者:

df[cols]=df[cols].fillna(mode.iloc[0])

您的解决方案:

df[cols]=df.filter(cols).fillna(mode.iloc[0])

样品:

df = pd.DataFrame({'workclass':['Private','Private',np.nan, 'another', np.nan],
                   'native-country':['United-States',np.nan,'Canada',np.nan,'United-States'],
                   'col':[2,3,7,8,9]})

print (df)
   col native-country workclass
0    2  United-States   Private
1    3            NaN   Private
2    7         Canada       NaN
3    8            NaN   another
4    9  United-States       NaN

mode = df.filter(["workclass", "native-country"]).mode()
print (mode)
  workclass native-country
0   Private  United-States

cols = ["workclass", "native-country"]
df[cols]=df[cols].fillna(df.mode().iloc[0])
print (df)
   col native-country workclass
0    2  United-States   Private
1    3  United-States   Private
2    7         Canada   Private
3    8  United-States   another
4    9  United-States   Private
3hvapo4f

3hvapo4f2#

你可以这样做:

df[["workclass", "native-country"]]=df[["workclass", "native-country"]].fillna(value=mode.iloc[0])

例如,

import pandas as pd
d={
    'key3': [1,4,4,4,5],
    'key2': [6,6,4],
    'key1': [6,4,4],
}

df=pd.DataFrame.from_dict(d,orient='index').transpose()

df

key3  key2    key1
0   1   6       6
1   4   6       4
2   4   4       4
3   4   NaN     NaN
4   5   NaN     NaN

然后通过执行以下操作:

l=df.filter(["key1", "key2"]).mode()
df[["key1", "key2"]]=df[["key1", "key2"]].fillna(value=l.iloc[0])

我们得到df

key3  key2    key1
0   1   6        6
1   4   6        4
2   4   4        4
3   4   6        4
4   5   6        4
vmpqdwk3

vmpqdwk33#

我认为使用dict作为fillna参数'value'是最简洁的方法
参考:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html
从@miriam-farber的响应创建一个玩具df

import pandas as pd
d={
    'key3': [1,4,4,4,5],
    'key2': [6,6,4],
    'key1': [6,4,4],
}

d_df=pd.DataFrame.from_dict(d,orient='index').transpose()

颁布法令

mode_dict = d_df.loc[:,['key2','key1']].mode().to_dict('records')[0]

在fillna方法中使用此dict

d_df.fillna(mode_dict, inplace=True)
k3bvogb1

k3bvogb14#

此代码将平均值插补到int列,将模式插补到object列,生成两种类型列的列表,并根据条件插补缺失值。

cateogry_columns=df.select_dtypes(include=['object']).columns.tolist()
integer_columns=df.select_dtypes(include=['int64','float64']).columns.tolist()

for column in df:
    if df[column].isnull().any():
        if(column in cateogry_columns):
            df[column]=df[column].fillna(df[column].mode()[0])
        else:
            df[column]=df[column].fillna(df[column].mean)`
nbnkbykc

nbnkbykc5#

您也可以使用SimpleImputer来解决此问题,如下所示:

from sklearn.impute import SimpleImputer

imputer = SimpleImputer(strategy='most_frequent', missing_values=np.nan)
df[["workclass", "native-country"]] = imputer.fit_transform(df[["workclass", "native-country"]])

相关问题