将pandas数据框中的列表转换为行

时间:2019-07-16 15:39:12

标签: python pandas dataframe

我有一个pd.Dataframe,其中包含如下列表元素。我想在新行中使用每个列表元素。

import pandas as pd
raw_data = {'score': [1,2,3], 
        'tags': [['apple','pear','guava'],['truck','car','plane'],['cat','dog','mouse']]}

df = pd.DataFrame(raw_data, columns = ['score', 'tags'])

>>> t
   score                  name
0      1  [apple, pear, guava]
1      2   [truck, car, plane]
2      3     [cat, dog, mouse]

# Conversion of list element to row

>>> t
   score        name
0      1       apple
1      1        pear
2      1       guava
3      2       truck
4      2         car
5      2       plane
6      3         cat
7      3         dog
8      3       mouse

我尝试了.apply(pd.Series)for循环的不同版本,但是只能将列表元素分成单独的columns(从来没有rows)。

预先感谢:)

0 个答案:

没有答案