1

I am having a problem with filtering a pandas dataframe. I am trying to filter a dataframe based on column values being equal to a specific list but I am getting a length error.

I tried every possible way of filtering a dataframe but got nowhere. Any help would be appreciated, thanks in advance.

Here is my code :

for ind in df_hourly.index:
    
    timeslot = df_hourly['date_parsed'][ind][0:4] # List value to filter
    filtered_df = df.loc[df['timeslot'] == timeslot]

Error : ValueError: ('Lengths must match to compare', (5696,), (4,))

Above Image : df , Below Image : df_hourly

enter image description here

In the above image, the dataframe I want to filter is shown. Specifically, I want to filter according to the "timeslot" column.

And the below image shows the the dataframe which includes the value I want to filter by. I specifically want to filter by "date_parsed" column. In the first line of my code, I iterate through every row in this dataframe and assign the first 4 elements of the list value in df_hourly["date_parsed"] to a variable and later in the code, I try to filter the above dataframe by that variable.

1
  • please show us some code with the way you tried it. also some example data what you are trying to achieve would be helpful. Commented Oct 28, 2022 at 11:04

1 Answer 1

1

When comparing columns using ==, pandas try to compare value by value - aka does the first item equals to first item, second item to the second and so on. This is why you receive this error - pandas expects to have two columns of the same shape.

If you want to compare if value is inside a list, you can use the .isin (documentation):

df.loc[df['timeslot'].isin(timeslot)]

Depends on what timeslot is exactly, you might to take timeslot.values or something like that (hard to understand exactly without giving an example for your dataframe)

Sign up to request clarification or add additional context in comments.

2 Comments

I tried that method but it returns an empty dataframe. Timeslot variable in my code is a list of integers
Please give full reproduceable example

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.