gpt4 book ai didi

python - 访问没有名称的列并从数据框中删除某些数据

转载 作者:太空宇宙 更新时间:2023-11-03 19:51:01 25 4
gpt4 key购买 nike

我有一个包含 14 列的数据框,其中没有任何标题。第一列是日期,我需要删除从现在起超过 25 个月且不包含任何日期的所有行。

                        44.93442    -79.37061   Tow 36  45.06541    -79.43384   R103    2053    WL7 CCG     
44.96092 -78.39428 Flatbed Tow 56 0 0 P201 3040 SHOP CCG
45.05056 -78.50632 Jump Start/Battery Test 30 45.12152 -78.61311 P201 1426 FB1 CCG
45.15709 -77.91703 Winch/Extrication 20 0 0 K143 957 SHOP CCG
45.24038 -81.64227 Tow 36 45.25674 -81.65996 W444 1597 WL1 CCG
45.32589 -78.98001 Winch/Extrication 79 0 0 R105 43 SHOP CCG
45.33402 -79.20586 Tow 38 45.32871 -79.21218 R103 226 WL10 CCG
46.2062 -82.47623 Tow 68 46.18153 -82.96333 R812 588 FM1 CCG
46.50164 -84.28004 Winch/Extrication 36 46.53398 -84.35199 R829 10 WL1 CCG
46.50694 -84.32854 No service 27 46.53578 -84.38477 R829 124 WL1 CCG
46.51246 -84.33669 No service 728 46.50897 -84.3295 R829 123 FB1 CCG
46.52354 -84.38171 Flatbed Tow 66 46.51035 -84.25437 R829 49 FB2 CCG
2017-11-11 00:03:10 43.68109 -79.53289 Tow 33 43.54982 -79.71832 C1 2 WL2 CCG 00:05:50
2017-11-11 00:04:09 43.91352 -78.9673 Tow 24 43.93242 -78.86989 C207 4 M255 CCG 00:04:11
2017-11-11 00:05:09 42.93152 -81.19933 Tow 161 42.91458 -81.2216 G72 5 FB6 CCG P5 00:16:21
2017-11-11 00:05:14 44.26861 -76.57645 Jump Start/Battery Test 21 44.26594 -76.49862 K112 6 LS1 CCG 00:12:23
2017-11-11 00:07:09 43.30024 -79.90106 Flatbed Tow 53 43.30506 -79.90795 H935 8 350 CCG P5 00:14:35
2017-11-11 00:07:14 43.71246 -79.55377 Tow 40 43.7081 -79.5536 C100 9 WL25 CCG P2 00:07:22

到目前为止我已经尝试过了,但还没有取得任何成功。

import pandas as pd
from datetime import datetime,timedelta

#find the 25 month prior month from now

N = 761
today = datetime.now()
lastMonth = today-timedelta(days=N)
month24= lastMonth.strftime("%Y-%m")
print (month24)
data = pd.read_csv("E:/ERS_DATA_TEMP.txt",delimiter='\t', dtype=str)

#access the dataframe and column

x=data.iloc[:,0]
print (x)
#remove data
data.drop(data[data.iloc[:,0]],[np.NaN]>=month24)
y=len(data.index)

最佳答案

读入不带标题的数据

df = pd.read_csv("E:/ERS_DATA_TEMP.txt",delimiter='\t', dtype=str, header=None)

立即删除所有行

df = df[df[0].apply(pd.to_datetime,errors='coerce').notnull()]

最后切开日期

df = df[df[0] > 月份24]

关于python - 访问没有名称的列并从数据框中删除某些数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59844608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com