如果行中包含N个值,则从列表中删除该行。

问题描述 投票:0回答:1

大家早上好

我在python中读取一个有多列的csv。

第一列是日期,我必须删除2017年之前的年份对应的行。

           time      high        low  Volume      Plot     Rango
0    2017-12-22  25.17984  24.280560     970  0.329943  0.899280
1    2017-12-26  25.17984  23.381280    2579  1.057921  1.798560
2    2017-12-27  25.17984  23.381280    2499  0.998083  1.798560
3    2017-12-28  25.17984  24.280560    1991  0.919885  0.899280
4    2017-12-29  25.17984  24.100704    2703  1.237694  1.079136
..          ...       ...        ...     ...       ...       ...
580  2020-04-16   5.45000   4.450000  117884  3.168380  1.000000
581  2020-04-17   5.35000   4.255200   58531  1.370538  1.094800
582  2020-04-20   4.66500   4.100100   25770  0.582999  0.564900
583  2020-04-21   4.42000   3.800000   20914  0.476605  0.620000
584  2020-04-22   4.22000   3.710100   23212  0.519275  0.509900

我想删除2018年之前的年份对应的行,所以应该删除2017,2016,2015...。

我试图用这个,但不工作

if 2017 in datos['time']: datos['time'].remove()  #check if number 2017 is in each of the items of the column 'time'

日期被识别为数字,而不是数据时间,但我认为我不需要将其声明为数据时间。

有什么帮助吗?谢谢!!!!

list csv row remove-if
1个回答
0
投票

pandas

  • 鉴于你的数据
  • 使用 布尔运算索引
  • time 必须 datetime64[ns] 格式化
    • df.info() 会给 dtypes
    • df['date'] = pd.to_datetime(df['date'])
df[df['time'].dt.year >= 2018]
© www.soinside.com 2019 - 2024. All rights reserved.