简体   繁体   中英

Select rows from Python DataFrame

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?

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"]

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)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM