为什么.str方法改变了大熊猫的形状?系列?

时间:2017-07-24 19:55:14

标签: python pandas dataframe

数据的类型

    In [1]: print(type(ebola_melt))
    <class 'pandas.core.frame.DataFrame'>

创建感兴趣的列,例如

    In [2]: ebola_melt['str_split'] = ebola_melt['type_country']
    .str.split('_')
    In [3]: print(type(ebola_melt['str_split']))
    <class 'pandas.core.series.Series'> 
应用了.str方法的

.get(0)

    In [4]: ebola_melt['str_split'].str.get(0)
    Out[4]: 
    0        Cases
    1        Cases
    2        Cases
    3        Cases
    4        Cases
        ...  

.get(0)未应用.str方法

    In [5]: ebola_melt['str_split'].get(0)
    Out[5]: ['Cases', 'Guinea']

1 个答案:

答案 0 :(得分:0)

pandas.Series.str.get从Series / Index中的列表中提取元素。

pandas.Series.get从对象获取给定索引值的项目

请参阅文档 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.get.htmlhttps://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.get.html

所以,

    ebola_melt['str_split']  #contains  ['Cases', 'Guinea']
    ebola_melt['str_split'].str.get(0) #returns the values present in the zeorth index of all rows which is Cases
    ebola_melt['str_split'].get(0) #returns the row present in the zeorth index 
    ebola_melt['str_split'].get #returns the all the rows