0

I'm very very new to coding so I apologize if my questioning format is incorrect. I'm pretty sure my problem is simple, I'm just not sure how to approach it.

I have a CSV file called SouthKoreaRoads2, and I used the following code.

import pandas as pd 
import os
SouthKoreaRoads2 = pd.read_csv("SouthKoreaRoads2.csv")

This is what my CSV file looks like

As you can see, there are dates in the second column. I need to extract the rows with dates under 1975. How should I proceed with this? Many thanks in advance, and any and all suggestions are very welcome! :)

Bopy
  • 23
  • 1
  • 4

2 Answers2

1

filter your data frame by date column.

df = SouthKoreaRoads2[SouthKoreaRoads2['DateColumn'] < 1975]

or

df = SouthKoreaRoads2.query('DateColumn< 1975') 

assuming that the column is named DateColumn

Mutaz-MSFT
  • 756
  • 5
  • 20
  • My column is called Simplified Date, and I did df = SouthKoreaRoads2[SouthKoreaRoads2['Simplified Date'] < 1975] but he response was a name error that SouthKoreaRoads2 is not defined. What would u recommend? – Bopy Jul 19 '21 at 08:53
  • your code example shows that the dataframe is called `SouthKoreaRoads2`, unless you changed it, this should work – Mutaz-MSFT Jul 19 '21 at 08:57
0

try this one

SouthKoreaRoads2[SouthKoreaRoads2[1] < 1975]

the 1 in SouthKoreaRoads2[1] takes your second column, if you have a column name associated just replace with SouthKoreaRoads2['the column name']

or if you like working with, let's say, the coordinates you can use this one as well:

SouthKoreaRoads2[SouthKoreaRoads2.iloc[:, 1] < 1975]

iloc[:, 1] - takes all the rows of the second column

Azat Aleksanyan
  • 140
  • 1
  • 14
  • Wow, thank you SouthKoreaRoads2[SouthKoreaRoads2['Simplified Date'] < 1975] worked perfectly. One more follow-up question, if I wanted to now extract the ones between 1975 and 1990, how would I set up that? – Bopy Jul 19 '21 at 08:58
  • @Bopy either you can do just double filtering, firstly >1975 after <1990 or for a more advanced way you can refer the answer here: https://stackoverflow.com/questions/29370057/select-dataframe-rows-between-two-dates – Azat Aleksanyan Jul 19 '21 at 09:08
  • Double filtering was a really helpful suggestion, thank you! – Bopy Jul 19 '21 at 09:13