Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫,按日期索引_Python_Pandas - Fatal编程技术网

Python 熊猫,按日期索引

Python 熊猫,按日期索引,python,pandas,Python,Pandas,我有以下熊猫数据框 1 2015_04_19_00_00_00 2 2015_04_19_01_00_00 3 2015_04_19_02_00_00 4 2015_04_19_03_00_00 5 2015_04_19_04_00_00 6 2015_04_19_05_00_00 7 2015_04_19_06_00_00 8 2020_06_10_00_00_00 9 2020_06_10_01_00_00 10 2020_06_10_02_00_00

我有以下熊猫数据框

1   2015_04_19_00_00_00
2   2015_04_19_01_00_00
3   2015_04_19_02_00_00
4   2015_04_19_03_00_00
5   2015_04_19_04_00_00
6   2015_04_19_05_00_00
7   2015_04_19_06_00_00
8   2020_06_10_00_00_00
9   2020_06_10_01_00_00
10  2020_06_10_02_00_00
11  2020_06_10_03_00_00
12  2020_06_10_04_00_00
13  2020_06_10_05_00_00
14  2030_04_15_01_00_00
15  2030_04_15_02_00_00
16  2030_04_15_10_00_00
17  2030_04_15_11_00_00
18  2040_05_29_01_00_00
19  2040_05_29_02_00_00
20  2040_05_29_03_00_00
21  2040_05_29_04_00_00
22  2040_05_29_05_00_00
23  2040_05_29_06_00_00
24  2040_05_29_07_00_00
25  2040_05_29_08_00_00
如何查询索引在哪一年发生变化

最终的结果应该是

2015    1
2020    8
2030    14
2040    18
这里有一条路

In [148]: s = df.time_col.str.split('_').str[0]

In [149]: idx = s[s.ne(s.shift())]

In [150]: idx
Out[150]:
1     2015
8     2020
14    2030
18    2040
Name: time, dtype: object

In [151]: pd.Series(idx.index, idx.values)
Out[151]:
2015     1
2020     8
2030    14
2040    18
dtype: int64
通过
~
与反转布尔掩码一起使用:

a = df.col.str.split('_').str[0]
#for improve performance
#a = pd.Series([x.split('_')[0] for x in df.col], index=df.index)

b = a[~a.duplicated()]
print (b)
1     2015
8     2020
14    2030
18    2040
Name: col, dtype: object

print(pd.Series(b.index, b.values))
2015     1
2020     8
2030    14
2040    18
dtype: int64

将来,您应该在这里发布您当前解决问题的尝试。