guopengfa
发布于 2022-09-29 / 1445 阅读 / 0 评论 / 0 点赞

Pandas Groupby Notes

to deil with dataframe to be any type what you want

import pandas as pd
import datetime


def data_sub(df: pd.DataFrame):
    days = 0
    if df.shape[0] == 2:
        days = (df['A'].max() - df['A'].min()).days
    df.loc[:, 'days'] = days
    df.drop(axis=1, columns=['A'], inplace=True)
    df.drop_duplicates(subset=['B'], inplace=True)
    return df.iloc[-1, :]


if __name__ == '__main__':
    df_data = pd.DataFrame({'A': [datetime.date(2020, 1, 1), datetime.date(2021, 3, 4), datetime.date(2020, 1, 1)],
                            'B': [1, 1, 2]})
    d = df_data.groupby(['B'], as_index=False).apply(lambda x: data_sub(x))
    print(d)

评论