简体   繁体   English

如何使用熊猫从excel数据文件中排除未来日期?

[英]How to exclude future dates from excel data file using pandas?

I'm trying to limit my dataset to dates before today.我试图将我的数据集限制在今天之前的日期。 Below creates a graph but the mask doesn't have any impact.下面创建了一个图表,但掩码没有任何影响。 Any help appreciated.任何帮助表示赞赏。

df = pd.read_excel("./data/Weight.xlsx", sheet_name='Data')
    
date = df.iloc[:, 0]
kilos = df.iloc[:, 3]

date = pd.to_datetime(date, format='%Y-%m-%d')
 
mask = (date < pd.to_datetime('today'))
df = df.loc[mask]
df.head()

   2021-02-06 00:00:00    18    2  115.208
42          2022-12-11  14.0  4.0   90.716
43          2022-12-18  14.0  0.0   88.900
44          2022-12-25  14.0  0.0   88.900
45          2023-01-01  14.0  2.0   89.808
46          2023-01-08  14.0  1.0   89.354

please check if you have a header in your excel file.请检查您的excel文件中是否有标题。 The df may not have a header. df可能没有标题。 Also, assuming the numbers - 41, 42, ... are all indeces.另外,假设数字 - 41, 42, ... 都是 indeces。 This appears to work fine with making header=None while reading the file.这似乎可以在读取文件时使header=None正常工作。

Input excel is here输入excel在这里输入

The updated code is here.更新的代码在这里。

df = pd.read_excel("input.xlsx", sheet_name='Data', header=None)
    
date = df.iloc[:, 0]
kilos = df.iloc[:, 3]

date = pd.to_datetime(date, format='%Y-%m-%d')
 
mask = (date < pd.to_datetime('today'))
df = df.loc[mask]

Output looks fine.输出看起来不错。 Here is what I am getting...这就是我得到的...

>> df
    0   1   2   3
0   2021-02-06  18  2   115.208

>> mask
0     True
1    False
2    False
3    False
4    False
5    False
Name: 0, dtype: bool

Thanks, that fixed it for me.谢谢,这为我解决了。

Chris克里斯

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

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