Panda和Python相对较新-我的语法可能缺少一些内容。我想在数据集中选择一个数据范围:
df1 = pd.read_csv("2020_Transactions_v03.txt", delimiter="|")
df1["Transaction Date"] = pd.to_datetime(df1["Transaction Date"])
start_date = '2020-03-01'
end_date = '2020-04-01'
mask = (df1['Transaction Date'] > start_date) & (df1("Transaction Date") <= end_date)
print(df1.loc[mask])
这实际上并没有达到我的预期(首先仅过滤转换数据类型所需的日期)。关于如何仅选择遮罩中的日期的任何想法?
答案 0 :(得分:0)
我不知道您的2020_Transactions_v03.txt
文件是什么样子,所以我创建了自己的DataFrame
:
import pandas as pd
# my own dataframe
dates = ['2020-04-01', '2020-04-02', '2020-04-03', '2020-04-04']
df = pd.DataFrame({'Transaction Date':dates})
start_date = '2020-04-01'
end_date = '2020-04-03'
# filtering
df_filtered = df[(df['Transaction Date'] > start_date) & (df['Transaction Date'] < end_date)]
print(df_filtered)
# output:
# Transaction Date
# 1 2020-04-02