Selecting multiple columns in a pandas dataframe

后端 未结 19 1792
醉话见心
醉话见心 2020-11-22 00:08

I have data in different columns but I don\'t know how to extract it to save it in another variable.

index  a   b   c
1      2   3   4
2      3   4   5
         


        
19条回答
  •  渐次进展
    2020-11-22 00:22

    you can also use df.pop()

    >>> df = pd.DataFrame([('falcon', 'bird',    389.0),
    ...                    ('parrot', 'bird',     24.0),
    ...                    ('lion',   'mammal',   80.5),
    ...                    ('monkey', 'mammal', np.nan)],
    ...                   columns=('name', 'class', 'max_speed'))
    >>> df
         name   class  max_speed
    0  falcon    bird      389.0
    1  parrot    bird       24.0
    2    lion  mammal       80.5
    3  monkey  mammal 
    
    >>> df.pop('class')
    0      bird
    1      bird
    2    mammal
    3    mammal
    Name: class, dtype: object
    
    >>> df
         name  max_speed
    0  falcon      389.0
    1  parrot       24.0
    2    lion       80.5
    3  monkey        NaN
    

    let me know if this helps so for you , please use df.pop(c)

提交回复
热议问题