【问题标题】:Reshape Pandas DatafRames by binary columns value通过二进制列值重塑 Pandas DatafRames
【发布时间】:2020-12-15 10:16:19
【问题描述】:

无法弄清楚如何通过几个二进制列值将我的 DataFrame 重塑为新的。

输入:

data         code    a  b   c

2016-01-07   foo     0  0   0
2016-01-12   bar     0  0   1
2016-01-03   gar     0  1   0
2016-01-22   foo     1  1   0
2016-01-26   bar     1  1   0

我想通过二进制值重塑,即列 a/b/c,如果它们的值 == 1,我每次都需要新列 包含所有数据。

预期输出:

         data       code        
  a   2016-01-22    foo      
  a   2016-01-26    bar       
  b   2016-01-03    gar     
  b   2016-01-22    foo      
  b   2016-01-26    bar       
  c   2016-01-12    bar       

从早上就卡在这里,非常感谢帮助!

【问题讨论】:

    标签: python pandas dataframe numpy reshape


    【解决方案1】:

    boolean indexing 中使用DataFrame.melt 与过滤1DataFrame.pop 用于过滤后去除列:

    df = df.melt(['data','code'], var_name='type')
    df = df[df.pop('value').eq(1)]
    print (df)
              data code type
    3   2016-01-22  foo    a
    4   2016-01-26  bar    a
    7   2016-01-03  gar    b
    8   2016-01-22  foo    b
    9   2016-01-26  bar    b
    11  2016-01-12  bar    c
    

    【讨论】:

    • 节省我的时间!我怎样才能添加额外的列,每种类型的数量都会出现? (对于所有 a - 2、b - 3、c - 1)?
    • @unkind58 - 当然,您可以查看this
    • @unkind58 - 意思是df['size'] = df.groupby(['type'])['type'].transform('size')
    • df['number'] = df['type'].map(dict(a=1,b=2,c=3))
    • @adirabargil - 它为新列重复计数
    猜你喜欢
    • 1970-01-01
    • 2018-11-26
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 2021-10-26
    • 1970-01-01
    • 2014-05-26
    相关资源
    最近更新 更多