python - 在 pandas 中制作具有不同大小的列表的 DataFrame

标签 python list pandas

我有这样的数据

genre_list
Out[7]: 
0                    [Action, Adventure, Fantasy, Sci-Fi]
1                            [Action, Adventure, Fantasy]
2                           [Action, Adventure, Thriller]
3                                      [Action, Thriller]
4                                           [Documentary]
5                             [Action, Adventure, Sci-Fi]
6                            [Action, Adventure, Romance]
7       [Adventure, Animation, Comedy, Family, Fantasy...
8                             [Action, Adventure, Sci-Fi]
9                   [Adventure, Family, Fantasy, Mystery]
10                            [Action, Adventure, Sci-Fi]
11                            [Action, Adventure, Sci-Fi]

我编码以制作具有不同大小的列表的数据框

genre_df = pd.DataFrame()
for i in range(len(genre_list)):
    genre_df = genre_df.append(pd.DataFrame(genre_list[i]).T)

得到这个

genre_df.head()
Out[9]: 
             0          1         2       3    4    5    6    7
0       Action  Adventure   Fantasy  Sci-Fi  NaN  NaN  NaN  NaN
0       Action  Adventure   Fantasy     NaN  NaN  NaN  NaN  NaN
0       Action  Adventure  Thriller     NaN  NaN  NaN  NaN  NaN
0       Action   Thriller       NaN     NaN  NaN  NaN  NaN  NaN
0  Documentary        NaN       NaN     NaN  NaN  NaN  NaN  NaN

有没有一种简单的方法来获取 Dataframe ....?

最佳答案

您可以使用 DataFrame 构造函数,通过 valuesgenre_list 值转换为 numpy array然后到列表:

df1 = pd.DataFrame(genre_list.values.tolist(), index=genre_list.index)
print (df1)

              0          1         2        3        4
0        Action  Adventure   Fantasy   Sci-Fi     None
1        Action  Adventure   Fantasy     None     None
2        Action  Adventure  Thriller     None     None
3        Action   Thriller      None     None     None
4   Documentary       None      None     None     None
5        Action  Adventure    Sci-Fi     None     None
6        Action  Adventure   Romance     None     None
7     Adventure  Animation    Comedy   Family  Fantasy
8        Action  Adventure    Sci-Fi     None     None
9     Adventure     Family   Fantasy  Mystery     None
10       Action  Adventure    Sci-Fi     None     None
11       Action  Adventure    Sci-Fi     None     None

如果需要将None替换为NaN:

df1 = pd.DataFrame(genre_list.values.tolist(), index=genre_list.index).replace({None:np.nan})
print (df1)
              0          1         2        3        4
0        Action  Adventure   Fantasy   Sci-Fi      NaN
1        Action  Adventure   Fantasy      NaN      NaN
2        Action  Adventure  Thriller      NaN      NaN
3        Action   Thriller       NaN      NaN      NaN
4   Documentary        NaN       NaN      NaN      NaN
5        Action  Adventure    Sci-Fi      NaN      NaN
6        Action  Adventure   Romance      NaN      NaN
7     Adventure  Animation    Comedy   Family  Fantasy
8        Action  Adventure    Sci-Fi      NaN      NaN
9     Adventure     Family   Fantasy  Mystery      NaN
10       Action  Adventure    Sci-Fi      NaN      NaN
11       Action  Adventure    Sci-Fi      NaN      NaN

另一个较慢的解决方案是apply Series:

df1 = genre_list.apply(pd.Series)
              0          1         2        3        4
0        Action  Adventure   Fantasy   Sci-Fi      NaN
1        Action  Adventure   Fantasy      NaN      NaN
2        Action  Adventure  Thriller      NaN      NaN
3        Action   Thriller       NaN      NaN      NaN
4   Documentary        NaN       NaN      NaN      NaN
5        Action  Adventure    Sci-Fi      NaN      NaN
6        Action  Adventure   Romance      NaN      NaN
7     Adventure  Animation    Comedy   Family  Fantasy
8        Action  Adventure    Sci-Fi      NaN      NaN
9     Adventure     Family   Fantasy  Mystery      NaN
10       Action  Adventure    Sci-Fi      NaN      NaN
11       Action  Adventure    Sci-Fi      NaN      NaN

时间:

#[12000 rows]
genre_list = pd.concat([genre_list]*1000).reset_index(drop=True)

In [115]: %timeit pd.DataFrame(genre_list.values.tolist(), index=genre_list.index).replace({None:np.nan})
100 loops, best of 3: 15.7 ms per loop

In [116]: %timeit df1 = genre_list.apply(pd.Series)
1 loop, best of 3: 1.96 s per loop

关于python - 在 pandas 中制作具有不同大小的列表的 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43134198/

相关文章:

python - 格式化 2D Numpy 数组 Float -> Percentage

list - 如何使用flutter dart中的相同元素初始化列表?

python - 输出 Pandas 数据框中所有列的数据

python - 如何使用 pandas 聚合具有空值的 bool 字段?

python - gtimelog 在 Mac OS 上找不到通过 Homebrew 安装的 pygtk

python - 将图像中的颜色范围更改为不同的颜色纹理

jquery sortable - 在更改时重新分配连续元素 ID

python - 在函数中使用变量

python - 所有 Pandas 数据类型

python - 如何将 groupby 结果分配给 pandas 中的系列