基于另一列的平均值填充一列的值

2024-10-02 04:23:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个熊猫数据帧。我试图根据区段列中相应级别的平均价格来填充价格列的nans。什么是一个高效而优雅的方法来做到这一点?我的数据看起来像这样

Name   Sex  Section  Price
Joe     M      1       2
Bob     M      1       nan
Nancy   F      2       5
Grace   F      1       6
Jen     F      2       3
Paul    M      2       nan

Tags: 数据方法namesection价格nan级别price
3条回答

所谓“相应的水平”,我假设你的意思是相等的截面值。在

如果是这样,你可以通过

for section_value in sorted(set(df.Section)):

    df.loc[df['Section']==section_value, 'Price'] = df.loc[df['Section']==section_value, 'Price'].fillna(df.loc[df['Section']==section_value, 'Price'].mean())

希望有帮助!和平

您可以使用组合groupbytransform和{}。请注意,我已经修改了您的示例,否则这两个部分的平均值相同。从开始

In [21]: df
Out[21]: 
    Name Sex  Section  Price
0    Joe   M        1    2.0
1    Bob   M        1    NaN
2  Nancy   F        2    5.0
3  Grace   F        1    6.0
4    Jen   F        2   10.0
5   Paul   M        2    NaN

我们可以利用

^{pr2}$

生产

In [23]: df
Out[23]: 
    Name Sex  Section  Price
0    Joe   M        1    2.0
1    Bob   M        1    4.0
2  Nancy   F        2    5.0
3  Grace   F        1    6.0
4    Jen   F        2   10.0
5   Paul   M        2    7.5

这是因为我们可以通过截面计算平均值:

In [29]: df.groupby("Section")["Price"].mean()
Out[29]: 
Section
1    4.0
2    7.5
Name: Price, dtype: float64

并将此广播回一个完整的系列,我们可以使用transform传递给fillna():

In [30]: df.groupby("Section")["Price"].transform("mean")
Out[30]: 
0    4.0
1    4.0
2    7.5
3    4.0
4    7.5
5    7.5
Name: Price, dtype: float64

pandas外科手术,但速度较慢

请参阅@DSM的答案以获得更快的pandas解决方案

这是一种更为外科手术的方法,可能提供一些视角,可能有用

  • 使用groupyby

    • 为每个Section计算我们的mean

      means = df.groupby('Section').Price.mean()
      
  • 识别空值

    • 使用isnull可用于布尔切片

      nulls = df.Price.isnull()
      
  • 使用map

    • Section列进行切片,将其限制为空Price的行

      fills = df.Section[nulls].map(means)
      
  • 使用loc

    • 只填充df中有空的地方

      df.loc[nulls, 'Price'] = fills
      

一起

means = df.groupby('Section').Price.mean()
nulls = df.Price.isnull()
fills = df.Section[nulls].map(means)
df.loc[nulls, 'Price'] = fills

print(df)

    Name Sex  Section  Price
0    Joe   M        1    2.0
1    Bob   M        1    4.0
2  Nancy   F        2    5.0
3  Grace   F        1    6.0
4    Jen   F        2   10.0
5   Paul   M        2    7.5

相关问题 更多 >

    热门问题