繁体   English   中英

根据特定条件在熊猫中添加新行? 蟒蛇

[英]adding a new row in pandas based on a certain condition? python

如果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'

像这样吗

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM