Skip to main content
 首页 » 编程设计

python之在 List 中具有不同大小的 List 的 pandas 中制作 DataFrame

2024年10月01日7java哥

我有这样的数据

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 构造函数将 genre_list 的值转换为 numpy array by values然后到 list:

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