我以某种方式得到了一个系列,其中索引作为元组,其中数据作为数字。我想通过删除 tuple[0] 值将其转换为一个索引为单个字符串的系列。 This is my current output Desired Output is something like this but in a series format
非常感谢。
最佳答案
您需要通过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
关于Python Pandas 系列元组数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43950463/