Python 对已分组的系列的索引执行操作
我想根据值对熊猫系列进行分组,然后对该系列的索引执行聚合操作。熊猫没有认出我通过ser.indexPython 对已分组的系列的索引执行操作,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() 如果您建议的解决方案是将该
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()