I'm trying to use the df.loc from pandas to locate a partially variable value in my dataframe.
in my dataframe there are times and dates combined for example
Time Col1 Col2
1-1-2019 01:00 5 7
1-1-2019 02:00 6 9
1-2-2019 01:00 8 3
if I use df.loc[df['Time'] == ['1-1-2019']]
I want to locate the first 2 columns of this dataframe that being 1-1-2019 01:00 and 1-1-2019 02:00.
it is giving my an error: Lengths must match . To be fair that is a logical error for pandas to give because I only am inputting the day, not the time.
Is there a way to make the search value partialy variable? So pandas look for the 1-1-2019 01:00 and 1-1-2019 02:00?
First idea is remove times, better say set to 0
by Series.dt.floor
or Series.dt.normalize
:
df['Time'] = pd.to_datetime(df['Time'])
df1 = df.loc[df['Time'].dt.floor('d') == '2019-01-01']
#alternative
#df1 = df.loc[df['Time'].dt.normalize() == '2019-01-01']
print (df1)
Time Col1 Col2
0 2019-01-01 01:00:00 5 7
1 2019-01-01 02:00:00 6 9
Or compare dates by Series.dt.date
:
from datetime import date
df['Time'] = pd.to_datetime(df['Time'])
df1 = df.loc[df['Time'].dt.date == date(2019,1,1)]
#in some version should working
#df1 = df.loc[df['Time'].dt.date == '2019-01-01']
Or convert to strings YYYY-MM-DD
by Series.dt.strftime
and compare:
df['Time'] = pd.to_datetime(df['Time'])
df1 = df.loc[df['Time'].dt.strftime('%Y-%m-%d') == '2019-01-01']
The following will subset the df on your condition
df[df.Time.str.contains('1-1-2019')]
Time Col1 Col2
0 2019-01-01 01:00:00 5 7
1 2019-01-01 02:00:00 6 9
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.