gpt4 book ai didi

python - 如何使用将每个元素从嵌套列表转换为数据框?

转载 作者:行者123 更新时间:2023-12-01 07:35:26 25 4
gpt4 key购买 nike

my_df = pd.DataFrame({'ID':['12345','23456','34567'],
'Info':[[['Rob Kardashian', '00052369', '1987-03-17', 'Reality Star'], ['Brooke Barry', '00213658', '2001-03-30', 'TikTok Star']],
[['Bae De Leon', '00896351', '1997-08-02', 'Volleyball Player'],['Jonas Blue', '02369785', '1990-08-02', 'Music Producer'],['Albert Einstein', '65231478', '1879-03-14','Scientist']],
[['Robert Downey Jr', '23897410', '1965-04-04', 'Actor'],['Stan Lee','35239856','1922-12-28','Publisher & Producer']]]})

enter image description here

大家好,我有上面的数据框,想要将“信息”列中的元素转换为行。我试过了

[[pd.DataFrame(i) for i in k] for k in my_df ['Info'].tolist()]

但是输出并不是我所期望的。

预期输出: enter image description here

预先感谢您的帮助!

最佳答案

您可以使用分组:

my_df.groupby("ID").Info.apply(lambda g: pd.DataFrame(g.iloc[0]))

这将为您聚合返回的数据帧:

>>> my_df.groupby("ID").Info.apply(lambda g: pd.DataFrame(g.iloc[0]))
0 1 2 3
ID
12345 0 Rob Kardashian 00052369 1987-03-17 Reality Star
1 Brooke Barry 00213658 2001-03-30 TikTok Star
23456 0 Bae De Leon 00896351 1997-08-02 Volleyball Player
1 Jonas Blue 02369785 1990-08-02 Music Producer
2 Albert Einstein 65231478 1879-03-14 Scientist
34567 0 Robert Downey Jr 23897410 1965-04-04 Actor
1 Stan Lee 35239856 1922-12-28 Publisher & Producer

然后您可以选择重置索引并删除 level_1 列:

expanded = my_df.groupby("ID").Info.apply(lambda g: pd.DataFrame(g.iloc[0]))
expanded.reset_index().drop("level_1", axis=1)

这给了你

      ID                 0         1           2                     3
0 12345 Rob Kardashian 00052369 1987-03-17 Reality Star
1 12345 Brooke Barry 00213658 2001-03-30 TikTok Star
2 23456 Bae De Leon 00896351 1997-08-02 Volleyball Player
3 23456 Jonas Blue 02369785 1990-08-02 Music Producer
4 23456 Albert Einstein 65231478 1879-03-14 Scientist
5 34567 Robert Downey Jr 23897410 1965-04-04 Actor
6 34567 Stan Lee 35239856 1922-12-28 Publisher & Producer

因为这使用了GroupBy.apply(),所以我不希望这会那么快。

将 Andy 和我的版本封装在函数中以运行时间试验确实表明使用我的版本将是较慢的选择:

In [99]: def np_concat(df):
...: df = df.set_index('ID')
...: pd.DataFrame(np.concatenate(my_df.Info), index=my_df.index.repeat(my_df.Info.str.len()))
...:

In [100]: def groupby(df):
...: df = df.groupby("ID").Info.apply(lambda g: pd.DataFrame(g.iloc[0]))
...: df.reset_index().drop("level_1", axis=1)
...:

In [101]: %timeit np_concat(my_df)
1.08 ms ± 107 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [102]: %timeit groupby(my_df)
6.33 ms ± 394 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

关于python - 如何使用将每个元素从嵌套列表转换为数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57011392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com