迭代 Pandas 中的行和列 [英] Iterating over rows and columns in Pandas

查看:49
本文介绍了迭代 Pandas 中的行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为列中的所有 NaN 值填充列的平均值.

I am trying to fill mean values of columns for all NaNs values in the column.

import numpy as np
import pandas as pd

table = pd.DataFrame({'A':[1,2,np.nan],
                  'B':[3,np.nan, np.nan],
                  'C':[4,5,6]})

def impute_missing_values(table):
    for column in table:
        for value in column:
            if value  == 'NaN':
                value = column.mean(skipna=True)
            else: 
                value = value
impute_missing_values(table)
table

为什么我收到此代码的错误?

Why I am getting an error for this code?

推荐答案

好的,我将这个添加为另一个答案,因为这根本不是我推荐的.使用 Pandas 方法将操作向量化以获得更好的性能.尽可能避免使用循环.

Okay, I am adding this as another answer because this isn't something I recommend at all. Using pandas methods vectorizes operations for better performance. Using loops is not recommended when possible to avoid.

但是,这里有一个快速修复您的代码:

However, here is a quick fix to your code:

import pandas as pd
import numpy as np
import math

table = pd.DataFrame({'A':[1,2,np.nan],
                  'B':[3,np.nan, np.nan],
                  'C':[4,5,6]})

def impute_missing_values(df):
    for column in df:
        for idx, value in df[column].iteritems():
            if math.isnan(value):
                df.loc[idx,column] = df[column].mean(skipna=True)
            else: 
                pass
    return df

impute_missing_values(table)
table

输出:

     A    B  C
0  1.0  3.0  4
1  2.0  3.0  5
2  1.5  3.0  6

这篇关于迭代 Pandas 中的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆