我有一个如下所示的 df :-
import pandas as pd
# intialise data of lists.
data = {'cust':['fnwp', 'utp'], 'events':[['abhi','ashu'],'abhi']}
# Create DataFrame
df = pd.DataFrame(data)
# Print the output.
df
我的预期结果是:-
最佳答案
您可以使用 pandas.explode() 函数:
>>> df.explode('events').reset_index(drop=True)
cust events
0 fnwp abhi
1 fnwp ashu
2 utp abhi
关于python - 如果数据框中的列有多个值,则创建多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65083064/