Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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,我想根据值对熊猫系列进行分组,然后对该系列的索引执行聚合操作。熊猫没有认出我通过ser.index dti = pd.date_range("2021-05-13", periods=6, freq="H") ser = pd.DataFrame({'Value': ['a', 'a', 'b', 'c', 'c', 'c']}, index=dti) ser.groupby('Value')[ser.index].min() 如果您建议的解决方案是将该

我想根据值对熊猫系列进行分组,然后对该系列的索引执行聚合操作。熊猫没有认出我通过ser.index

dti = pd.date_range("2021-05-13", periods=6, freq="H")
ser = pd.DataFrame({'Value': ['a', 'a', 'b', 'c', 'c', 'c']}, index=dti)
ser.groupby('Value')[ser.index].min()
如果您建议的解决方案是将该系列转换为DF,那么请注意,在我的实际用例中,我将上述内容合并到一个函数中,我将应用于大型数据帧的每一列。因此也不清楚如何将索引传递给每一列

dti = pd.date_range("2021-05-13", periods=6, freq="H")

col1 = pd.Series(['a', 'a', 'b', 'c', 'c', 'c'])
col2 = pd.Series(['a', 'c', 'b', 'a', 'c', 'a'])

df = pd.DataFrame({'col1': col1, 'col2': col2})
df.index = dti

df.groupby('col1')[df.index].min()

将索引转换为系列:

>>> df.index.to_series().groupby(df["col1"]).min()
col1
a   2021-05-13 00:00:00
b   2021-05-13 02:00:00
c   2021-05-13 03:00:00
dtype: datetime64[ns]

df.sort\u index()