Delete a range of values in python

Asked

Viewed 81 times

0

I have a code that eliminates values as needed, but I’m not able to put an interval to delete or keep the value. For example, I want values between 7:00 and 5:00.

If you put values greater than 18h, it accepts, but not both conditions at the same time. Follow the code:

import pandas as pd
dataset = pd.read_csv('/home/Downloads/Dados_PNBoia/boia_deriva/B116353_col_2018.csv')

type(dataset)

df = pd.concat([dataset], ignore_index=True)
df = dataset.loc[(dataset[' hour'] > 18) &  (dataset[' hour'] < 6)]
df.to_csv('/home/Downloads/Dados_PNBoia/boia_deriva/B116353_col_2018_noite.csv')

Someone knows how to fix?

P.S.: I know that in the case I put the symbol &, and in case it would be OR, but the same does not accept, says that:

'The Truth value of a Series is ambiguous'.

1 answer

2


His logic is reversed, or tells how we express the problems colloquially in Portuguese.

If you say programmatically - as above, you want only values greater than 18 and and values less than 6, the result is no value, because no number can be at the same time greater than 18 and less than 6.

You need a "or" condition - and in the world of Numpy + Pandas (which requires a slightly different way of thinking than using Python without using these libraries), this is done with the operator | (and not with the operator &). In Python code without involving actions vectorized by Numpy yes, the operator would be used or in full.

import pandas as pd
dataset = pd.read_csv('/home/Downloads/Dados_PNBoia/boia_deriva/B116353_col_2018.csv')

type(dataset)

df = pd.concat([dataset], ignore_index=True)
df = dataset.loc[(dataset[' hour'] > 18) | (dataset[' hour'] < 6)]
df.to_csv('/home/Downloads/Dados_PNBoia/boia_deriva/B116353_col_2018_noite.csv')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.