如何将带有值列表的列转换为 Pandas DataFrame 中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39954668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:10:27  来源:igfitidea点击:

How to convert column with list of values into rows in Pandas DataFrame

pythonpandasdataframe

提问by Agus Sanjaya

Hi I have a dataframe like this:

嗨,我有一个这样的数据框:

    A             B 
0:  some value    [[L1, L2]]

I want to change it into:

我想把它改成:

    A             B 
0:  some value    L1
1:  some value    L2

How can I do that?

我怎样才能做到这一点?

采纳答案by MaxU

you can do it this way:

你可以这样做:

In [84]: df
Out[84]:
               A               B
0     some value      [[L1, L2]]
1  another value  [[L3, L4, L5]]

In [85]: (df['B'].apply(lambda x: pd.Series(x[0]))
   ....:         .stack()
   ....:         .reset_index(level=1, drop=True)
   ....:         .to_frame('B')
   ....:         .join(df[['A']], how='left')
   ....: )
Out[85]:
    B              A
0  L1     some value
0  L2     some value
1  L3  another value
1  L4  another value
1  L5  another value

UPDATE:a more generic solution

更新:更通用的解决方案

回答by Pygirl

Pandas >= 0.25

Pandas >= 0.25

df1 = pd.DataFrame({'A':['a','b'],
               'B':[[['1', '2']],[['3', '4', '5']]]})
print(df1)


    A   B
0   a   [[1, 2]]
1   b   [[3, 4, 5]]


df1 = df1.explode('B')
df1.explode('B')


    A   B
0   a   1
0   a   2
1   b   3
1   b   4
1   b   5

I don't know how good this approach is but it works when you have a list of items.

我不知道这种方法有多好,但是当您有项目列表时它会起作用。

回答by jezrael

Faster solution with chain.from_iterableand numpy.repeat:

使用chain.from_iterable和更快的解决方案numpy.repeat

from itertools import chain
import numpy as np
import pandas as pd

df = pd.DataFrame({'A':['a','b'],
                   'B':[[['A1', 'A2']],[['A1', 'A2', 'A3']]]})

print (df)
   A               B
0  a      [[A1, A2]]
1  b  [[A1, A2, A3]]


df1 = pd.DataFrame({ "A": np.repeat(df.A.values, 
                                    [len(x) for x in (chain.from_iterable(df.B))]),
                     "B": list(chain.from_iterable(chain.from_iterable(df.B)))})

print (df1)
   A   B
0  a  A1
1  a  A2
2  b  A1
3  b  A2
4  b  A3

Timings:

时间

A = np.unique(np.random.randint(0, 1000, 1000))
B = [[list(string.ascii_letters[:random.randint(3, 10)])] for _ in range(len(A))]
df = pd.DataFrame({"A":A, "B":B})
print (df)
       A                                 B
0      0        [[a, b, c, d, e, f, g, h]]
1      1                       [[a, b, c]]
2      3     [[a, b, c, d, e, f, g, h, i]]
3      5                 [[a, b, c, d, e]]
4      6     [[a, b, c, d, e, f, g, h, i]]
5      7           [[a, b, c, d, e, f, g]]
6      8              [[a, b, c, d, e, f]]
7     10              [[a, b, c, d, e, f]]
8     11           [[a, b, c, d, e, f, g]]
9     12     [[a, b, c, d, e, f, g, h, i]]
10    13        [[a, b, c, d, e, f, g, h]]
...
...

In [67]: %timeit pd.DataFrame({ "A": np.repeat(df.A.values, [len(x) for x in (chain.from_iterable(df.B))]),"B": list(chain.from_iterable(chain.from_iterable(df.B)))})
1000 loops, best of 3: 818 μs per loop

In [68]: %timeit ((df['B'].apply(lambda x: pd.Series(x[0])).stack().reset_index(level=1, drop=True).to_frame('B').join(df[['A']], how='left')))
10 loops, best of 3: 103 ms per loop

回答by Howardyan

I can't find a elegant way to handle this, but the following codes can work...

我找不到一种优雅的方法来处理这个问题,但以下代码可以工作......

import pandas as pd
import numpy as np
df = pd.DataFrame([{"a":1,"b":[[1,2]]},{"a":4, "b":[[3,4,5]]}])
z = []
for k,row in df.iterrows():
    for j in list(np.array(row.b).flat):
        z.append({'a':row.a, 'b':j})
result = pd.DataFrame(z)

回答by Adrian P

I think this is the fastest and simplest way:

我认为这是最快和最简单的方法:

df = pd.DataFrame({'A':['a','b'],
               'B':[[['A1', 'A2']],[['A1', 'A2', 'A3']]]})


df.set_index('A')['B'].apply(lambda x: pd.Series(x[0]))

回答by oli5679

Here's another option

这是另一种选择

unpacked = (pd.melt(df.B.apply(pd.Series).reset_index(),id_vars='index')
 .merge(df, left_on = 'index', right_index = True))
unpacked = (unpacked.loc[unpacked.value.notnull(),:]
.drop(columns=['index','variable','B'])
.rename(columns={'value':'B'})
  1. Apply pd.series to column B --> splits each list entry to a different row
  2. Melt this, so that each entry is a separate row (preserving index)
  3. Merge this back on original dataframe
  4. Tidy up - drop unnecessary columns and rename the values column
  1. 将 pd.series 应用于 B 列 --> 将每个列表条目拆分为不同的行
  2. 融化这个,这样每个条目都是一个单独的行(保留索引)
  3. 将其合并回原始数据帧
  4. 整理 - 删除不必要的列并重命名值列