jaayjoo
jaayjoo

Reputation: 21

How can I print certain rows from a CSV in pandas

My problem is that I have a big dataframe with over 40000 Rows and now I want to select the rows from 2013-01-01 00:00:00 until 2013-31-12 00:00:00

print(df.loc[df['localhour'] == '2013-01-01 00:00:00'])

Thats my code now but I can not choose an intervall for printing out ... any ideas ?

Upvotes: 1

Views: 275

Answers (2)

jpp
jpp

Reputation: 164673

One way is to set your index as datetime and then use pd.DataFrame.loc with string indexers:

df = pd.DataFrame({'Date': ['2013-01-01', '2014-03-01', '2011-10-01', '2013-05-01'],
                   'Var': [1, 2, 3, 4]})

df['Date'] = pd.to_datetime(df['Date'])

res = df.set_index('Date').loc['2010-01-01':'2013-01-01']

print(res)

            Var
Date           
2013-01-01    1
2011-10-01    3

Upvotes: 1

harpan
harpan

Reputation: 8631

Make a datetime object and then apply the condition:

print(df)

date
0   2013-01-01
1   2014-03-01
2   2011-10-01
3   2013-05-01

df['date']=pd.to_datetime(df['date'])
df['date'].loc[(df['date']<='2013-12-31 00:00:00') & (df['date']>='2013-01-01 00:00:00')]

Output:

0   2013-01-01
3   2013-05-01

Upvotes: 0

Related Questions