熊猫数据框中的字符串列表列表更改为长格式

时间:2018-09-20 12:50:29

标签: pandas list dataframe itertools

我无法完全重现我的示例,请想象列表中的每个元素都用引号引起来并且是一个字符串变量。我不确定这是否会影响总体答案,但想添加该信息。

给出:

lis1= [['apples'],['bananas','oranges','cinnamon'],['pears','juice']]
lis2= [['john'],['stacy'],['ron']]
lis3= [['2015-11-24'], ['2014-02-23','2014-03-25', '2014-03-29'],['2018-02-01','2018-03-27']]
lis4= [['smells good'],['saweet','sour as hell','spicey is goody'],['it bites back','so good']]


pd.DataFrame({'fruits':lis1,'users':lis2, 'date': lis3, 'review': lis4})

我需要:

lis1= ['apples','bananas','oranges','cinnamon','pears','juice']
lis2= ['john','stacy', 'stacy','stacy','ron','ron']
lis3= ['2015-11-24', '2014-02-23', '2014-03-25', '2014-03-29','2018-02-01', '2018-03-27']
lis4= ['smells good','saweet','sour as hell','spicey is goody','it bites back','so good']


pd.DataFrame({'fruits':lis1,'users':lis2, 'date': lis3, 'review': lis4})

我试图改编Itertools示例,但无法弄清楚要用4列做到这一点。

1 个答案:

答案 0 :(得分:0)

我认为需要列表理解扁平化的产品:

import ast

#if necessary convert strings to lists
#df = df.applymap(ast.literal_eval)

from  itertools import product
df1 = pd.DataFrame([j for i in df.values for j in product(*i)], columns=df.columns)

print (df1)
      fruits  users        date           review
0     apples   john  2015-11-24      smells good
1    bananas  stacy  2014-02-23           saweet
2    bananas  stacy  2014-02-23     sour as hell
3    bananas  stacy  2014-02-23  spicey is goody
4    bananas  stacy  2014-03-25           saweet
5    bananas  stacy  2014-03-25     sour as hell
6    bananas  stacy  2014-03-25  spicey is goody
7    bananas  stacy  2014-03-29           saweet
8    bananas  stacy  2014-03-29     sour as hell
9    bananas  stacy  2014-03-29  spicey is goody
10   oranges  stacy  2014-02-23           saweet
11   oranges  stacy  2014-02-23     sour as hell
12   oranges  stacy  2014-02-23  spicey is goody
13   oranges  stacy  2014-03-25           saweet
14   oranges  stacy  2014-03-25     sour as hell
15   oranges  stacy  2014-03-25  spicey is goody
16   oranges  stacy  2014-03-29           saweet
17   oranges  stacy  2014-03-29     sour as hell
18   oranges  stacy  2014-03-29  spicey is goody
19  cinnamon  stacy  2014-02-23           saweet
20  cinnamon  stacy  2014-02-23     sour as hell
21  cinnamon  stacy  2014-02-23  spicey is goody
22  cinnamon  stacy  2014-03-25           saweet
23  cinnamon  stacy  2014-03-25     sour as hell
24  cinnamon  stacy  2014-03-25  spicey is goody
25  cinnamon  stacy  2014-03-29           saweet
26  cinnamon  stacy  2014-03-29     sour as hell
27  cinnamon  stacy  2014-03-29  spicey is goody
28     pears    ron  2018-02-01    it bites back
29     pears    ron  2018-02-01          so good
30     pears    ron  2018-03-27    it bites back
31     pears    ron  2018-03-27          so good
32     juice    ron  2018-02-01    it bites back
33     juice    ron  2018-02-01          so good
34     juice    ron  2018-03-27    it bites back
35     juice    ron  2018-03-27          so good