Python Pandas:从系列中获取str

时间:2014-11-12 10:25:47

标签: python pandas series

我想从系列中获得重复的字符串。

这是我的示例代码。

from pandas import DataFrame,Series

d = {'Sex' : Series(["Male","Male","Female","Female","Female","Male","Female","Female","Male"]),
      'Rank' : Series(["A","B","C","A","B","C","A","B","C"]),
      'Num' : Series([1,2,3,4,5,6,4,5,6]),
      'States' : Series(["Ohio","Ohio","Ohio","Ohio","Ohio","Ohio","Ohio","Ohio","Ohio"])}

df = DataFrame(d)
States = df.States.drop_duplicates()

print "This is "+States

我可以获得包含Name:和dtype:

的字符串
  

0这是俄亥俄州

     

名称:States,dtype:object

我只想选择"俄亥俄州"从它。

感谢阅读!

1 个答案:

答案 0 :(得分:1)

States = df.drop_duplicates(['States'])

然后像

print "This is " + States.States[0]

会为您提供您正在寻找的结果。