简体   繁体   English

int' 对象不可迭代", '发生在索引 i

[英]int' object is not iterable", 'occurred at index i

I`m trying to apply a .mean function in a dataframe 'combined_sf2' which contains the shape '43722 rows × 62 columns'.我正在尝试在包含形状“43722 行 × 62 列”的数据帧“combined_sf2”中应用 .mean 函数。

I would like to calculate the mean of some values in a range of different attributes from my dataframe for each row.我想从我的数据框中为每一行计算一系列不同属性中某些值的平均值。 Then generating a new attribute/column with the name 'wkQtyEXTMean', which will contain the mean of this selected range of attributes values for each row.然后生成一个名为“wkQtyEXTMean”的新属性/列,其中将包含每行的此选定属性值范围的平均值。

I tried to create the follow function applying the .mean function from statistics method:我尝试使用统计方法中的 .mean 函数创建跟随函数:

    #function create to take the range of the selected attributes, if the sum is zero, so return the message 'thre is no mean', if not, calculate the mean
    
import statistics
    def wkQtyEXTMean(row):
        if (row['wk13QtyEXT']+row['wk12QtyEXT']) == 0:
            return 'No mean'
       else:
            return statistics.mean(row['wk13QtyEXT']+row['wk12QtyEXT'])

    #generating new column
    combined_sf2['wkQtyEXTMean'] = combined_sf2.apply(wkQtyEXTMean, axis=1)

But i`m getting the follow error:但我收到以下错误:

("'int' object is not iterable", 'occurred at index 43721')

Expected result预期结果在此处输入图片说明

Any suggestion what should I do ?有什么建议我该怎么办?

Please find updated answer.请找到更新的答案。

# Online Python compiler (interpreter) to run Python online.

import pandas as pd
import statistics
# Creating the dataframe 
df = pd.DataFrame({"A":[12, 4, 5, None, 1],
                   "B":[7, 2, 54, 3, None],
                   "C":[20, 16, 11, 3, 8],
                   "D":[14, 3, None, 2, 6]})
  
# skip the Na values while finding the mean
print(df.mean(axis = 1, skipna = True))
te = df.mean(axis = 1, skipna = True)
print(statistics.mean(te))

0 13.250000 0 13.250000

1 6.250000 2 23.333333 1 6.250000 2 23.333333

3 2.666667 3 2.666667

4 5.000000 4 5.000000

dtype: float64数据类型:float64

This is answer.这是答案。 10.1 10.1

Old conversation:旧对话:

This is the syntax for mean.这是 mean 的语法。

# Importing the statistics module
import statistics
  
# list of positive integer numbers
data1 = [1, 3, 4, 5, 7, 9, 2]
  
x = statistics.mean(data1)

Now what you are doing wrong is现在你做错的是

return statistics.mean(row['wk13QtyEXT']+row['wk12QtyEXT']) is wrong
 
return statistics.mean(row) is right

Try (your approach):尝试(你的方法):

import statistics

def wkQtyEXTMean(row):
    if row['wk13QtyEXT'] == 0 and row['wk12QtyEXT'] == 0:
        return 'No mean'
    return statistics.mean([row['wk13QtyEXT'], row['wk12QtyEXT']])

combined_sf2['wkQtyEXTMean'] = combined_sf2.apply(wkQtyEXTMean, axis=1)

Or without statistics.mean :或者没有statistics.mean

def wkQtyEXTMean(row):
    if row['wk13QtyEXT'] == 0 and row['wk12QtyEXT'] == 0:
        return 'No mean'
    return (row['wk13QtyEXT'] + row['wk12QtyEXT']) / 2

combined_sf2['wkQtyEXTMean'] = combined_sf2.apply(wkQtyEXTMean, axis=1)

Or without apply :或不apply

combined_sf2['wkQtyEXTMean'] = (combined_sf2['wk13QtyEXT']
                                + combined_sf2['wk12QtyEXT']) / 2
combined_sf2.loc[combined_sf2['wk13QtyEXT'].eq(0)
                 & combined_sf2['wk12QtyEXT'].eq(0), 'wkQtyEXTMean'] = 'No mean'

Or if you are already using NumPy ( np ):或者,如果您已经在使用 NumPy ( np ):

combined_sf2['wkQtyEXTMean'] = np.where(
            combined_sf2['wk13QtyEXT'].eq(0) & combined_sf2['wk12QtyEXT'].eq(0),
            'No mean',
            (combined_sf2['wk13QtyEXT'] + combined_sf2['wk12QtyEXT']) / 2
        )

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

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