繁体   English   中英

如何获取索引作为值,而不是 pandas 系列中的元组

[英]How to get the index as a value, not a tuple from a pandas series

我有一个名为 valueCounts 的 pandas 系列:

In: valueCounts
Out[58]: 
StartDate 
1986-01-02      22
1986-01-03      25
1986-01-06      26
1986-01-07      28
1986-01-09      29
              ... 
2022-07-06    1947
2022-07-11    1948
2022-07-14    1949
2022-07-18    1951
2022-07-22    1952
Length: 680, dtype: int64

我可以将索引作为元组列表获取:

In: valueCounts.index
Out[59]: 
MultiIndex([('1986-01-02',),
            ('1986-01-03',),
            ('1986-01-06',),
            ('1986-01-07',),
            ('1986-01-09',),
            ('1987-09-01',),
            ('1987-09-10',),
            ('1988-09-13',),
            ('1988-10-26',),
            ('1989-10-26',),
            ...
            ('2022-06-20',),
            ('2022-06-21',),
            ('2022-06-28',),
            ('2022-07-01',),
            ('2022-07-04',),
            ('2022-07-06',),
            ('2022-07-11',),
            ('2022-07-14',),
            ('2022-07-18',),
            ('2022-07-22',)],
           names=['StartDate'], length=680)

我怎样才能将元组列表作为时间戳或字符串列表,或者其他什么,而不是元组?

以下是如何执行此操作的示例:

import pandas as pd

valueCounts = pd.DataFrame(
    {
        "StartDate": [
            "2022-01-01",
            "2022-01-02",
            "2022-01-03",
            "2022-01-02",
            "2022-01-03",
            "2022-01-02",
            "2022-01-03",
            "2022-01-01",
            "2022-01-01",
            "2022-01-01",
        ]
    }
).value_counts()

print(valueCounts)
# Output
StartDate 
2022-01-01    4
2022-01-02    3
2022-01-03    3
dtype: int64
indices = valueCounts.index.get_level_values(0).to_list()

print(indices)
# Output
['2022-01-01', '2022-01-02', '2022-01-03']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM