在Pandas中根据特定条件添加新行?python

2024-07-01 06:36:36 发布

您现在位置:Python中文网/ 问答频道 /正文

可以用吗pd.应用()如果isinstance(x,list)为True,则添加新行 例如,我可以在同一个数据帧中将idx57分成三个不同的行吗?在

目前:

56    the University of Notre Dame
57    [Berkeley, Columbia Business School, Haas]

期望:

^{pr2}$

Tags: ofthe数据truebusiness中将listisinstance
1条回答
网友
1楼 · 发布于 2024-07-01 06:36:36

像这样?在

In [106]: s = pd.Series(["the University of Notre Dame", ["Berkeley", "Columbia Business School", "Haas"]])

In [107]: s
Out[107]: 
0                  the University of Notre Dame
1    [Berkeley, Columbia Business School, Haas]
dtype: object

In [110]: s.apply(pd.Series).stack().reset_index(drop=True)
Out[110]: 
0    the University of Notre Dame
1                        Berkeley
2        Columbia Business School
3                            Haas
dtype: object

相关问题 更多 >

    热门问题