在pandas中根据特定条件添加新行?python
可以使用pd.apply()来添加新行吗?如果x是列表的话,那就可以。例如,我能把索引57的那一行变成数据框中的三行吗?
现在的情况是:
56 the University of Notre Dame
57 [Berkeley, Columbia Business School, Haas]
想要的结果是:
56 the University of Notre Dame
57 Berkeley
58 Columbia B School
59 Haas
AttributeError Traceback (most recent call last)
<ipython-input-1121-0cd6253a0f30> in <module>()
15 s = pd.Series(["the University of Notre Dame", ["Berkeley", "Columbia Business School", "Haas"]])
16
---> 17 s.apply(lambda x : pd.Series(x)).stack().reset_index(drop=True)
AttributeError: 'Series' object has no attribute 'stack'
1 个回答
0
像这样吗?
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