Pandas 如何按日期排序DataFrame

Pandas 如何按日期排序DataFrame

Python的Pandas DataFrame定义了由行和列组成的二维结构。Pandas的主要特点是更简单地处理给定的数据。在Python中,我们有一些内置函数,如to_datetime()、sorted()、lambda和sort_value()来按日期排序一个Pandas DataFrame。

语法

以下语法用于示例-

to_datetime()

The to_datetime() 是Python的内置函数,用于将字符串日期转换为日期时间对象。

sorted()

Python的内置函数sorted()说明可以对列表进行按照指定的可迭代对象进行排序。

lambda

这个Python中的lambda函数被称为匿名函数。在需要函数对象的时候可以使用它。这个函数操作程序的主要逻辑部分。

sort_values()

sort_values()是Python中的内置函数,用于按升序和降序对数据框进行排序。

使用sort_values()函数

在下面的示例中,我们将首先导入pandas模块,该模块将用于处理按日期进行数据框。然后创建变量d来定义输入字典。接下来,使用内置函数DataFrame()设置数据框,并将其存储在变量df中。然后使用内置函数to_datetime设置日期的格式,并将其存储在变量df[‘Date’]中。然后初始化名为Date_result的最后一个变量,将值存储为df.sort_value(),该函数接受两个参数- ‘Date’和ascending=True,以按升序对数据框进行排序。最后,通过变量Date_result打印结果。

示例

import pandas as pd
# Create a sample DataFrame
d = { 'Date': ['2023-06-26', '2023-06-27', '2023-06-28', '2023-06-29'], 'Value': [100, 200, 300, 400] }
# Set the dataframe for date
df = pd.DataFrame(d)
# Convert the 'Date' column to datetime format
df['Date'] = pd.to_datetime(df['Date'])
# Sort the dataframe in ascending order
Date_result = df.sort_values('Date', ascending=True)
# The final output
print("Result of pandas Dataframe by Date:\n", Date_result)

输出

Result of pandas Dataframe by Date:
         Date  Value
0 2023-06-26    100
1 2023-06-27    200
2 2023-06-28    300
3 2023-06-29    400

使用sorted()函数

在下面的示例中,程序使用了两个主要方法,即列表推导式和sorted()函数作为内置函数DataFrame()的参数来生成结果。

示例

import pandas as pd
# Create a sample DataFrame
d = { 'Date': ['2023-06-26', '2023-06-27', '2023-06-28', '2023-06-29'], 'Value': [11, 31, 456, 8534] }
# Create the dataframe
df = pd.DataFrame(d)
# Conversion of date into datetime format
df['Date'] = pd.to_datetime(df['Date'])
# using list comprehension and sorted()
res = pd.DataFrame(sorted(df.values, key=lambda x: x[0]), columns=df.columns)
# The final output
print("Result of pandas Dataframe by Date:\n", res)

输出

Result of pandas Dataframe by Date:
         Date  Value
0 2023-06-26     11
1 2023-06-27     31
2 2023-06-28    456
3 2023-06-29   8534

使用sort_values()中的inplace参数

在以下示例中,程序使用内置函数to_datetime(),通过参数df[‘date’]将日期列的格式设置为datetime格式。接下来,它将使用内置函数sort_values(),它接受两个参数- Date和inplace = true,它意味着它按日期对数据框进行排序。

示例

import pandas as pd
# Create a sample DataFrame
data = { 'Date': ['2023-06-15', '2023-06-14', '2023-06-17', '2023-06-16'], 'Value': [10, 20, 30, 40] }
df = pd.DataFrame(data)
# Convert the 'Date' column to datetime format
df['Date'] = pd.to_datetime(df['Date'])
# Sort the DataFrame by the Date column using the parameter inplace
df.sort_values('Date', inplace=True)
print(df)

输出

Date  Value
1 2023-06-14     20
0 2023-06-15     10
3 2023-06-16     40
2 2023-06-17     30

使用to_datetime()函数

在以下示例中,我们将使用内置的DataFrame()函数,该函数接受参数作为变量数据。使用此函数将数据转换为数据帧。数据帧通常将数据创建为2维。接下来,使用内置的to_datetime()函数设置给定输入日期的格式并生成结果。

示例

import pandas as pd
# Create a sample DataFrame
data = { 'Date': ['2023-06-15', '2023-06-14', '2023-06-17', '2023-06-16'], 'Value': [10, 20, 30, 40], 'Attendance': ['Present', 'Present', 'Absent', 'Absent'] }
# Create the dataframe for date
df = pd.DataFrame(data)
# Convert the 'Date' column to datetime format
df['Date'] = pd.to_datetime(df['Date'])
print("The Dataframe of Date result:\n", df)

结果

The Dataframe of Date result:
         Date  Value Attendance
0 2023-06-15     10    Present
1 2023-06-14     20    Present
2 2023-06-17     30     Absent
3 2023-06-16     40     Absent

结论

Pandas是一个代表Python顶级库的开源库。在本文中,我们讨论了它使用一些内置函数(例如to_datetime(), DataFrame()等)来满足特定条件和操作的各种方法。这种类型的程序通常用于数据科学领域。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程