简体   繁体   English

Pandas:如何按一列的日期对数据框行进行排序

[英]Pandas: How to sort dataframe rows by date of one column

So I have two different data-frame and I concatenated both. 所以我有两个不同的数据框架,我连接了两个。 All columns are the same; 所有列都相同; however, the date column has all sorts of different dates in the M/D/YR format. 但是,日期列具有M / D / YR格式的各种不同日期。

在此输入图像描述 dataframe dates get shuffled around later in the sequence 数据帧日期在序列中稍后变得混乱

Is there a way to keep the whole dataframe itself and just sort the rows based on the dates in the date column. 有没有办法保留整个数据框本身,只是根据日期列中的日期对行进行排序。 I also want to keep the format that date is in. 我还想保留日期所在的格式。

so basically 所以基本上

date        people
6/8/2015    1
7/10/2018   2
6/5/2015    0

gets converted into: 转换成:

date          people
6/5/2015      0
6/8/2015      1
7/10/2018     2

Thank you! 谢谢!

PS: I've tried the options in the other post on this but it does not work PS:我已经尝试了其他帖子中的选项,但它不起作用

Trying to elaborate on what can be done: Intialize/ Merge the dataframe and convert the column into datetime type 试着详细说明可以做什么:初始化/合并数据帧并将列转换为datetime类型

df= pd.DataFrame({'people':[1,2,0],'date': ['6/8/2015','7/10/2018','6/5/2015',]})
df.date=pd.to_datetime(df.date,format="%m/%d/%Y")
print(df)

Output: 输出:

   date      people
0   2015-06-08  1
1   2018-07-10  2
2   2015-06-05  0

Sort on the basis of date 按日期排序

df=df.sort_values('date')
print(df)

Output: 输出:

    date    people
2   2015-06-05  0
0   2015-06-08  1
1   2018-07-10  2

Maintain the format again: 再次维护格式:

df['date']=df['date'].dt.strftime('%m/%d/%Y')
print(df)

Output: 输出:

    date    people
2   06/05/2015  0
0   06/08/2015  1
1   07/10/2018  2

Try changing the 'date' column to pandas Datetime and then sort 尝试将“日期”列更改为pandas Datetime,然后排序

import pandas as pd
df= pd.DataFrame({'people':[1,1,1,2],'date': 
['4/12/1961','5/5/1961','7/21/1961','8/6/1961']})
df['date'] =pd.to_datetime(df.date)
df.sort_values(by='date')

Output: 输出:

date       people

1961-04-12  1

1961-05-05  1

1961-07-21  1

1961-08-06  2

To get back the initial format: 要获取初始格式:

df['date']=df['date'].dt.strftime('%m/%d/%y')

Output: 输出:

date    people
04/12/61    1

05/05/61    1

07/21/61    1

08/06/61    2

Not sure exactly what you want to get but if you just want to get people who belong to one date just simply use groupby . 不知道你想要什么得到,但如果你只是想谁属于一个 约会,只是简单地使用groupby

df = df.groupby('date').sum()

or different groupby 或者不同的组合

df = df.groupby('date').agg(lambda col: col.tolist()).reset_index()

Then you can sort it as you want. 然后你可以根据需要对其进行排序。 Maybe this is going to be what you're looking for Sort Pandas Dataframe by Date 也许这将是你想要的按日期排序Pandas Dataframe

why not simply? 为什么不简单?

dataset[SortBy["date"]]

can you provide what you tried or how is your structure? 你可以提供你尝试过的东西或你的结构如何?

In case you need to sort in reversed order do: 如果您需要按相反顺序排序,请执行以下操作:

dataset[SortBy["date"]][Reverse]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM