分组数据框并根据条件在其中选择一个单元格

时间:2019-01-16 13:11:42

标签: python pandas dataframe

我的数据集看起来像这样 Tr, Date, Time AV81312,20161014,121000 AV81312,20161014,160221 AV85012,20170422,150858 AV85012,20161108,11137 AV86157,20170426,45747 AV86157,20170426,45744 AV86157,20160813,134312

我只需要从每个Tr中选择一个具有最新记录的项目,即具有更高日期和时间的最新记录

必需的输出是 Tr, Date, Time AV81312,20161014,160221 AV85012,20170422,150858 AV86157,20170426,45747

我的代码是

df2 = read_csv("sample.csv") df2 = df2.values x = [] for i in df2: for j in df2: if i[2] == j[2]: if i[3] >= j[3]: x.append(i) 它没有按预期工作

1 个答案:

答案 0 :(得分:1)

使用-

protected void lv_positions_i_manage_paging(object sender, PagePropertiesChangingEventArgs e)
{
    (lv_positions_i_manage.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    if (LastSortDirection.Text == "asc")
    {
        LastSortDirection.Text = "desc";
    }
    else
    {
        LastSortDirection.Text = "asc";
    }

        BindListview("2", LastSortField.Text);
}

输出

df['Date_Time'] = pd.to_datetime(df['Date'].astype(str).str.cat(df['Time'].astype(str)),format='%Y%m%d%H%M%S')
df.loc[df.groupby('Tr')['Date_Time'].idxmax()].drop('Date_Time', axis=1)