0

I have got a Python DataFrame called "x" like this:

363108 05:01:00

363107 05:02:00

363106 05:03:00

363105 05:04:00

363104 05:05:00

        ...   

4 16:57:00

3 16:58:00

2 16:59:00

1 17:00:00

0 17:01:00

The "time" column is string type.

I want to create a new DataFrame called "m" from all the rows in "x" such that the minute is "00".

I have tried m = x.loc[x["time"][3:5] == "00"] but I get "IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match)."

Does anybody know how to do this please?

3
  • Have you tried converting your time string into timestamp using pandas and then use the "resample" function at the minute level ? Commented Sep 23, 2020 at 19:06
  • I dont even know what youre talking about. I guess that the solution to my problem is more simple. Commented Sep 23, 2020 at 19:08
  • if you are not familiar with pandas time series you can use REGEX : x.str.replace('(:\d{2})',':00'). Commented Sep 23, 2020 at 19:32

2 Answers 2

1

You should use "apply" for the condition.

x.loc[x["time"].apply(lambda s: s[3:5] == "00")]

*In your code you are getting the range [3:5] on time Series(row 3 to 5)

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

1 Comment

Perfect. It worked like a charm. I was gettin rows 3 to 5 in x, thats why the code didnt work. Thanks a lot.
1

One way can be that you can create a new column in the existing dataframe that has the minutes field, which you can slice from the time column

df['minutes']=df['time'][-2:]
other_df=df.loc[df['minutes']=="00"]

1 Comment

Thanks Syed. I think this could work too, but the answer from GuiGNG is simpler and worked like a charm. Thanks anyway.

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.