简体   繁体   English

Python dataframe 带日期(日期和事件)

[英]Python dataframe with date (dates and events)

Using PYTHON, I am trying to sort the values in the table based on dates and the associated events.使用 PYTHON,我试图根据日期和相关事件对表中的值进行排序。 Date1 and event1 are a pair because event1 occurred on date1. Date1 和 event1 是一对,因为 event1 发生在 date1。 The same for other dates and events.其他日期和事件也一样。 date1 is in a column of its own, and event1 is also in its own separate column. date1 在它自己的列中,event1 也在它自己的单独列中。

For example, the table would include as below:例如,该表将包括如下内容:

date1日期 1 event1事件1 date2日期2 event2事件2 date3日期3 event3事件3
March 6, 2021 2021 年 3 月 6 日 eventC事件C Jan. 1, 2020 2020 年 1 月 1 日 eventX事件X May 11, 2020 2020 年 5 月 11 日 eventB事件B
Dec. 6, 2021 2021 年 12 月 6 日 eventBB事件BB Feb. 11, 2001 2001 年 2 月 11 日 eventYY事件YY June 13, 1990 1990 年 6 月 13 日 eventSS事件SS
March 16, 2019 2019 年 3 月 16 日 eventCD活动CD Jan. 1, 1998 1998年1月1日 eventRE事件RE May 23, 1989 1989 年 5 月 23 日 eventDF事件DF
Nov. 1, 2008 2008 年 11 月 1 日 eventCC事件CC April 14, 2001 2001 年 4 月 14 日 eventWQ事件WQ March 17, 1999 1999 年 3 月 17 日 eventCV事件CV

I would like the result to show as follows where for each row, sort the data from oldest date to the newest, but the order of the events follow their respective dates (eg eventC that occurred on March 6, 2021 as the first in row 0 is now reordered to be the third in row 0).我希望结果显示如下,对于每一行,将数据从最早日期到最新日期排序,但事件的顺序遵循它们各自的日期(例如,2021 年 3 月 6 日发生的 eventC 作为第 0 行中的第一个现在重新排序为第 0 行中的第三个)。

date1日期 1 event1事件1 date2日期2 event2事件2 date3日期3 event3事件3
Jan. 1, 2020 2020 年 1 月 1 日 eventX事件X May 11, 2020 2020 年 5 月 11 日 eventB事件B March 6, 2021 2021 年 3 月 6 日 eventC事件C
June 13, 1990 1990 年 6 月 13 日 eventSS事件SS Feb. 11, 2001 2001 年 2 月 11 日 eventYY事件YY Dec. 6, 2021 2021 年 12 月 6 日 eventBB事件BB
May 23, 1989 1989 年 5 月 23 日 eventDF事件DF Jan. 1, 1998 1998年1月1日 eventRE事件RE March 16, 2019 2019 年 3 月 16 日 eventCD活动CD
March 17, 1999 1999 年 3 月 17 日 eventCV事件CV Nov. 1, 2008 2008 年 11 月 1 日 eventCC事件CC April 14, 2001 2001 年 4 月 14 日 eventWQ事件WQ

I would like to keep the output in the table format as above.我想将 output 保留在上面的表格格式中。 (unless there is a good reason not to...) =) (除非有充分的理由不...)=)

This is actually a very small version of a much larger data.这实际上是更大数据的一个非常小的版本。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks to all in advance!!!提前感谢大家!!!

Cast the ' Dates ' values to datetime and use the sort_values() function. Example:将“ Dates ”值转换为datetime时间并使用sort_values() function。示例:

import pandas as pd

df = pd.DataFrame([{"Date":"02-01-1945","Name":"event2"},{"Date":"20-11-1934", "Name":"event1"}])
print(df)

#Output:
#
#         Date    Name
#0  02-01-1945  event2
#1  20-11-1934  event1

df["Date"] = pd.to_datetime(df["Date"])
df= df.sort_values(by="Date")
print(df)

#Output:
#        Date    Name
#1 1934-11-20  event1
#0 1945-02-01  event2

暂无
暂无

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

相关问题 计算python中数据框中每个日期的事件数 - Counting number of events on each date in a dataframe in python Python:将 Dataframe 从日期列表转换为 Date From & Date To 格式 - Python: Transform Dataframe from list of dates to a Date From & Date To format 给定一个包含事件详细信息的 dataframe,根据事件的开始和结束日期返回在任何给定日期发生的事件计数 - Given a dataframe with event details, return a count of events that occured on any given date, based on the start and end dates of the events 如何在Python中过滤带有其他日期的日期数据框? - How can I filter a dataframe of dates with other date in Python? 如果另一个 Python pandas dataframe 中的两个日期之间的日期,则更新列 - Update column if date between 2 dates in another Python pandas dataframe Python Pandas 如何比较一个 Dataframe 中的日期与另一个 ZC699575A5E8AFD9E22A7ECC8CAB 中的日期? - Python Pandas how to compare date from one Dataframe with dates in another Dataframe? 在 dataframe 中查找最接近另一个 dataframe 日期的日期 - Find the date in a dataframe that is closest to another dataframe of dates 如何用 python 中两个日期之间的随机日期替换 dataframe 的日期列 - how to replace a date column of a dataframe with a random date between two dates in python 在 dataframe 中循环,日期和日期之间在 python - cycle in a dataframe with dates and between dates in python 使用另一个事件的 dataframe 屏蔽日期的 pandas dataframe - Masking pandas dataframe of dates using another dataframe of events
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM