【问题标题】:Efficient filtering of columns高效过滤列
【发布时间】:2018-09-25 02:44:53
【问题描述】:

我有以下数据框:

item         = ['item1','item2','item3']
amount       = [500,200,800]
feature_2020 = [18,32,34]
feature_2030 = [28,42,44]
feature_2040 = [38,52,54]
df = pd.DataFrame({'index':item,'amount':amount,'feature_2020': feature_2020,'feature_2030':feature_2030,'feature_2040':feature_2040})
df.index= df['index']
print(df)



         amount   feature_2020  feature_2030  feature_2040  index
index                                                         
item1     500            18            28            38     item1
item2     200            32            42            52     item2
item3     800            34            44            54     item3

我想有效地执行以下操作(我已经可以做到,但方式非常糟糕):

  • 对于每个特征列(feature_2020、feature_2030、feature_2040),我想过滤严格低于 20、介于 20 和严格低于 40 以及高于 40 的值。
  • 应用此过滤器后,我想计算上面过滤的每个类别的剩余项目的数量列的总和。

预期结果:

inf20            = [500,1000,0]
supequal20_inf40 = [0,500,1000]
supequal40       = [0,500,1000]
index            = ['inf20','supequal20_inf40','supequal40']
result = pd.DataFrame({'sum_feature_2020':inf20,'sum_feature_2030':supequal20_inf40,'sum_feature_2040': supequal40,'index':index})
result.index= result['index']
print(result)



              sum_feature_2020  sum_feature_2030    sum_feature_2040                                                     
inf20               500                0                  0
supequal20_inf40    1000               500                500
supequal40           0                 1000               1000

有没有办法以有效的方式进行这种过滤和操作?

非常感谢您的帮助,

【问题讨论】:

  • feature_2030和feature_2040中的值属于同一类。为什么结果不同?
  • 抱歉,结果部分出错了,我把矩阵倒置了。我已经更新了帖子,结果很好。 @RafaelC

标签: python pandas dataframe filtering calculation


【解决方案1】:

首先我会融化它,将我们想要分箱的所有值放在一列中,然后我会使用 pd.cut 分箱它,然后我会转回来。

d2 = df.melt(["index", "amount"])
d2["binned"] = pd.cut(d2.value, [0, 20, 40, np.inf], right=False)
out = d2.pivot_table(index="binned", columns="variable",
                     values="amount", aggfunc=sum).fillna(0)

这给了我

In [172]: out
Out[172]: 
variable      feature_2020  feature_2030  feature_2040
binned                                                
[0.0, 20.0)          500.0           0.0           0.0
[20.0, 40.0)        1000.0         500.0         500.0
[40.0, inf)            0.0        1000.0        1000.0

通过

In [173]: d2
Out[173]: 
   index  amount      variable  value        binned
0  item1     500  feature_2020     18   [0.0, 20.0)
1  item2     200  feature_2020     32  [20.0, 40.0)
2  item3     800  feature_2020     34  [20.0, 40.0)
3  item1     500  feature_2030     28  [20.0, 40.0)
4  item2     200  feature_2030     42   [40.0, inf)
5  item3     800  feature_2030     44   [40.0, inf)
6  item1     500  feature_2040     38  [20.0, 40.0)
7  item2     200  feature_2040     52   [40.0, inf)
8  item3     800  feature_2040     54   [40.0, inf)

【讨论】:

    【解决方案2】:

    另一种方式,不太通用的方式

    x = df.set_index('amount')[[ 'feature_2020', 'feature_2030', 'feature_2040']]
    
    r1 = x.lt(20).mul(x.index, axis=0).sum()
    r2 = (x.ge(20) & x.lt(40)).mul(x.index, axis=0).sum()
    r3 = x.ge(40).mul(x.index, axis=0).sum()
    
    df_f = pd.concat([r1,r2,r3], 1).T
    
        feature_2020    feature_2030    feature_2040
    0   500             0               0
    1   1000            500             500
    2   0               1000            1000
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 2010-09-23
      • 2021-07-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多