1

I have the following dataframe

print(df.head(5))

                          date  places_occupees
0  2017-01-01 00:00:00.0000000              238
1  2017-01-01 00:01:00.0000000              238
2  2017-01-01 00:02:00.0000000              238
3  2017-01-01 00:03:00.0000000              238
4  2017-01-01 00:04:00.0000000              238
5  2017-01-01 00:05:00.0000000              238

(please note that the date column type is string)

I have a list of strings that I will use to sort data in the dataframe.

print(list_holidays)

['2017-01-01', '2017-05-01', '2017-05-08', '2017-07-14', '2017-11-11', '2017-04-17', '2017-06-05', '2017-05-25', '2017-08-15', '2017-11-01', '2017-12-25']

then I create a new empty dataframe with the same 2 columns. I will fill it with data using the loop:

new_df = pd.DataFrame(columns=['date', 'places_occupees'])

Here is what I used but returns an empty dataframe

for i in list_holidays:
    filter = df[df['date'].str.contains(i)]
    new_df['date'].append(filter.date)
    new_df['places_occupes'].append(filer.places_occupees)

What I would like to do is to fill the new_df 'date' column with the dates obtained after sorting and to fill the new_df 'places_occupees' column with the values obtained after sorting that should look like the initial dataframe but after applying a filter.

1
  • wait, why the need to create a dataframe and loop? what happens, for example, when there are multiple matches (like 2017-07-01) Commented Jul 16, 2019 at 10:10

1 Answer 1

1

you can save your filter results (which are correct) in a list, then use pd.concat to get the new df.

try this:

filtered = []
for i in list_holidays:
    filter = df[df['date'].str.contains(i)]
    filtered.append(filter)

new_df = pd.concat(filtered)

print(new_df)

or with a simple list-comprehension:

new_df = pd.concat([df[df['date'].str.contains(i)] for i in list_holidays])

print(new_df)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.