簡體   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