【问题标题】:How to collapse multiple rows into one and create series of column elements Python Pandas如何将多行折叠成一行并创建一系列列元素 Python Pandas
【发布时间】:2020-12-14 12:41:58
【问题描述】:

我有一个如下的数据框:

                tags    categories            classification
          0    label    ['legislative', 
                         'law, govt and 
                         politics', 'exe...        None
          0   document  ['legislative', 
                         'law, govt and politics', 
                            'exe...                 NaN
          0     text    ['legislative', 'law, 
                          govt and politics', 
                          'exe...                   NaN
          0     paper   ['legislative', 'law, 
                          govt and 
                        politics', 'exe...          NaN
          0     poster  ['legislative', 'law, 
                        govt and politics', 'exe... NaN
        

我想创建一个新的数据框,我可以将上面的数据框折叠成下面的数据框,以便将“标签”和“分类”列的列元素转换为具有列表格式的单个项目的单行,例如

                tags     categories           classification
       0   ['label',     ['legislative',      ['None','NaN',
           'document',  'law, govt and          'NaN','NaN',
             'text',          politics', 'exe...    'NaN']                
         'paper',poster']

我该怎么做呢?我如何使用堆栈或分组函数来获得结果?提前致谢。

*这是 df.to_dict() 的结果

           {'tags': {0: ' letter',
            1: ' head',
            2: ' water',
            3: ' art',
            4: ' indoors',
            5: ' flyer',
            6: ' poster',
            ...},
            'categories': {0: "['legislative', 'law, govt and politics', 
            'executive branch', 'work', 'society', 'government']",
            1: "['unrest and war', 'society', 'religion and spirituality', 
            'buddhism']",
            2: '[]',
            3: '[]',
            4: "['unemployment', 'society', 'law, govt and politics', 
            'foreign policy', 'work', 'politics', 'armed forces']",
            5: '[]',
            6: "['sports', 'law, govt and politics', 'wrestling']",
            ...},
            'classfication': {0: nan,
            1: nan,
            2: nan,
            3: nan,
            4: nan,
            5: nan,
            6: nan,
            ...}}

【问题讨论】:

  • 你能粘贴df.to_dict()的结果吗,df是你的数据框吗?
  • 你能说得更清楚点吗?我无法彻底理解你的任务。我想你也想折叠classification专栏吧?
  • @ipj 我已经粘贴了结果

标签: python pandas group-by jupyter-notebook series


【解决方案1】:

我没有完全理解你的问题。但是你想要这样的东西吗?

df:

    trial_num   subject samples
0   1           1       [-1.74, -0.78, -0.11]
1   2           1       [0.86, 0.21, -0.01]
2   3           1       [2.04, 0.6, -0.79]
3   1           2       [0.52, 0.49, 1.56]
4   2           2       [0.07, 0.84, -1.1]
5   3           2       [0.43, -1.3, 1.99]

转换后的df:

     trial_num          subject             samples
0   [1, 2, 3, 1, 2, 3]  [1, 1, 1, 2, 2, 2]  [[-1.74, -0.78, -0.11], [0.86, 0.21, -0.0...trial_num   subject samples
0   [1, 2, 3, 1, 2, 3]  [1, 1, 1, 2, 2, 2]  [[-1.74, -0.78, -0.11], [0.86, 0.21, -0.0...

import pandas as pd
df = pd.DataFrame(
    {'trial_num': [1, 2, 3, 1, 2, 3],
     'subject': [1, 1, 1, 2, 2, 2],
     'samples': [list(np.random.randn(3).round(2)) for i in range(6)]
    }
)
df = df.astype(str).apply(', '.join).apply(lambda x: x.split(',')).to_frame().T

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-20
    • 2019-08-07
    • 2015-03-29
    • 1970-01-01
    • 2015-01-31
    • 2020-06-09
    • 1970-01-01
    相关资源
    最近更新 更多