如何使用 Pandas 对 DataFrame 按日期排序?
Python 的 Pandas DataFrame 定义了一个由行和列组成的二维结构。Pandas 的主要特点是提供了一种更简单的方式来处理给定的数据。在 Python 中,我们有一些内置函数,例如 to_datetime()、sorted()、lambda 和 sort_value(),可以用来对 Pandas DataFrame 按日期排序。
语法
以下语法在示例中使用:
to_datetime()
to_datetime() 是 Python 中的一个内置函数,用于将字符串日期转换为日期时间对象。
sorted()
Python 的内置函数 sorted() 指出列表可以按指定的可迭代对象进行排序。
lambda
Python 中的 lambda 函数被称为匿名函数。当需要函数对象时可以使用它。此函数操作程序的主要逻辑部分。
sort_values()
sort_values() 是 Python 中的一个内置函数,用于按升序或降序对 DataFrame 进行排序。
使用 sort_values() 函数
在以下示例中,我们将从导入 pandas 模块开始程序,该模块将用于按日期处理 DataFrame。然后创建变量 d 来定义输入字典。接下来,使用内置函数 DataFrame() 设置 DataFrame 并将其存储在变量 df 中。然后使用内置函数 to_datetime 设置日期的格式,并将其存储在变量 df['Date'] 中。继续初始化名为 Date_result 的最后一个变量,该变量存储值为 df.sort_value() 的值,它接受两个参数 - 'Date' 和 ascending = True 以升序对 DataFrame 进行排序。最后,它将在 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'] 作为参数来设置日期时间格式的列。接下来,它将使用内置函数 sort_values(),它接受两个参数 - Date 和 inplace = true,这意味着它按日期对 DataFrame 进行排序。
示例
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(),它接受变量 data 作为参数。使用此函数将数据转换为 DataFrame。DataFrame 通常以二维方式创建数据。接下来,使用内置函数 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() 等,来满足特定条件和操作。这种类型的程序通常用于数据科学领域。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP