Mar*_*ars 2 python series dataframe pandas
我有:
df = pd.DataFrame(
{
"A": [["a", "b", "c"], ["d"]],
"B": [[1, 2, 3], [4]],
"C": [["abc"], ["def"]]
}
)
A B C
0 [a, b, c] [1, 2, 3] [abc]
1 [d] [4] [def]
Run Code Online (Sandbox Code Playgroud)
我的预期输出是:
A B C
0 a 1 abc
1 b 2 abc
2 c 3 abc
3 d 4 def
Run Code Online (Sandbox Code Playgroud)
我试过了
df = df.explode("A")
df = df.explode("B")
Run Code Online (Sandbox Code Playgroud)
但它创建了一个“组合产品”并保留了索引。
你可以用df.apply
与pd.Series.explode
df.apply(pd.Series.explode) #.reset_index(drop=True) If required.
A B C
0 a 1 abc
0 b 2 abc
0 c 3 abc
1 d 4 def
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
43 次 |
最近记录: |