【发布时间】:2014-06-03 02:41:12
【问题描述】:
python中是否有一个函数可以让我计算数组中非缺失值的数量?
我的数据:
df.wealth1[df.wealth < 25000] = df.wealth
df.wealth2[df.wealth <50000 & df.wealth > 25000] = df.wealth
df.wealth3[df.wealth < 75000 & df.wealth > 50000] = df.wealth
...
id, income, wealth, wealth1, wealth2, ... wealth9
1, 100000, 20000, 20000, ,...,
2, 60000, 40000, , 40000, ...,
3 70000, 23000, 23000, , ...,
4 80000, 75000, , ,..., 75000
...
我现在的情况:
income_brackets = [(0, 25000), (25000,50000), (50000,100000)]
source = {'wealth1': [], 'wealth2' :[], .... 'wealth9' : []
for lower, upper in income_brackets:
for key in source:
source[key].append(len(df.query('income > {} and income < {}'.format(lower,upper))[np.logical_not(np.isnan([key]))]))
但这不起作用,因为np.isnan('wealth1') 无效。它仅适用于np.isnan(df.wealth1),但我无法将其合并到我的 for 循环中。我对 python 很陌生,所以也许(希望)我遗漏了一些明显的东西。
任何建议或问题都会很棒。谢谢!干杯
【问题讨论】:
标签: python for-loop count pandas missing-data