Python Pandas 系列元组数据框

标签 python pandas dataframe tuples

我以某种方式得到了一个系列,其中索引作为元组,其中数据作为数字。我想通过删除 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/

相关文章:

python - 使用 ffmpeg-python : [NULL @ 000002486ae7b180] Unable to find a suitable output format for 获取错误消息

python - 将可迭代字典展开/扩展到数据框中

python - 根据列条件添加包含来自另一个数据框的值的列

python - 使用 `contains` 合并 DataFrame(不是完全匹配!)

pandas - 将数字上限/阈值应用于 pandas 数据框

python - Pandas Dataframe 合并而不复制任何一方?

python - 如何检查大数据集中Python中最大值的日期和时间

python TypeError 使用 timedelta 添加时间

python - 如何限制相同日志消息的数量?

python - 如何使用循环将一行除以前一行的内容