如何转换在每一列的每一行中包含列表的数据框
问题描述:
我有以下dataframe
,它是for循环的输出之一.
I have the following dataframe
which is one of the output from for loop.
df = pd.DataFrame()
df['Score'] = [['0-0','1-1','2-2'],['0-0','1-1','2-2']]
df ['value'] =[[0.08,0.1,0.15],[0.07,0.12,0.06]]
df ['Team'] = ['A','B']
我想将每一行的列表的每个元素转换为一列的每个元素. 以下是预期的输出.
I want to transform each element of list of each row to each element of a column. The following is the expected output.
任何人都可以帮助我进行变革吗?
Can anyone help me how to transform it?
谢谢
Zep
答
在每个数据帧列表上应用pd.Series
后,您可以尝试一次取消堆栈索引
You can try of unstacking index, once after applying pd.Series
on each list of dataframe
df = pd.DataFrame()
df['Score'] = [['0-0','1-1','2-2'],['0-0','1-1','2-2']]
df ['value'] =[[0.08,0.1,0.15],[0.07,0.12,0.06]]
df.stack().apply(pd.Series).ffill(1).unstack(level=0).T.reset_index(drop=True)
出局:
Score value Team
0 0-0 0.08 A
1 0-0 0.07 B
2 1-1 0.1 A
3 1-1 0.12 B
4 2-2 0.15 A
5 2-2 0.06 B