如果isinstance(x,list)为True,是否可以使用pd.apply()添加新行 那么,例如,我可以将idx 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'
答案 0 :(得分: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