Python Pandas Series Tuples dataframe



我不知何故得到了一个系列,索引作为元组,其中数据作为数字。我想通过删除元组[0]值将其转换为具有索引的单个字符串的系列。这是我当前的输出,所需的输出是这样的,但采用系列格式

多谢。

您需要

str[1]选择元组的第二个值:

s.index = s.index.str[1]

样本:

s = pd.Series([80,79,70], 
              index=[('total','Mumbai'),('total','Chennai'),('total','Royal')])
print (s)
(total, Mumbai)     80
(total, Chennai)    79
(total, Royal)      70
dtype: int64
s.index = s.index.str[1]
print (s)
Mumbai     80
Chennai    79
Royal      70
dtype: int64

map的另一种解决方案:

s.index = s.index.map(lambda x: x[1])
print (s)
Mumbai     80
Chennai    79
Royal      70
dtype: int64

最新更新