Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the earliest date from a column (Python Pandas) after csv.reader

I load some data from a CSV file with multiple columns. In my csv.reader I have an IF function. I am trying to get the earliest date from a specific column (Start Date).

I first load the data:

for row in csv.reader(open('myFile.csv')):
  if row[4] == '56886':
    key = row[4] #key = (row[4], row[33][:4], row[4])
    startDate = row[19]

When I print the column (startDate) I am getting this:

enter image description here

01) I tried using the below:

content = min(content)
print(content)

and I got this in terminal:

enter image description here

02) Then I tried changing my code:

for row in csv.reader(open('myFile.csv',
        parse_dates=['Start Date'], 
        usecols=['Start Date']))
  if row[4] == '56886':
    key = row[4] #key = (row[4], row[33][:4], row[4])
    startDate = row[19]

and I got an invalid syntax error.

03) I tried changing the line to:

pandas.read_csv('myFile.csv', parse_dates=['Start Date'], usecols=['Start Date'])

and I got the same error.

What is the best workaround? I have not found a solution so far.

like image 492
Datacrawler Avatar asked Jul 24 '26 14:07

Datacrawler


1 Answers

I think you need boolean indexing for filtering:

#dont filter all columns by usecols    
df = pd.read_csv('file', parse_dates=['Start Date', 'End Date']) #columns to datetimes

#filter output first by column ID and then get min and max
a = df.loc[ df['ID'] == 56886, 'Start Date'].min()


b = df.loc[ df['ID'] == 56886, 'End Date'].max()
like image 174
jezrael Avatar answered Jul 27 '26 04:07

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!