Home > Net >  Python: drop value=0 row in specific columns. If there is just one 0, delete whole row
Python: drop value=0 row in specific columns. If there is just one 0, delete whole row

Time:11-08

I know this question has been asked many times but it just doesnt work for me and i dont know why. I need to see a graph without zero values, but it only shows a graph with them no matter what i try

These are the first 5 rows from .csv file. I need this kind of values

0  2022-10-24T12:12:35Z  44.61
1  2022-10-24T12:12:40Z  17.33
2  2022-10-24T12:12:45Z  41.20
3  2022-10-24T12:12:51Z  33.49
4  2022-10-24T12:12:56Z  55.68


29944  2022-11-03T09:51:58Z    0.0
29945  2022-11-03T09:52:28Z    0.0
29946  2022-11-03T09:52:58Z    0.0
29947  2022-11-03T09:53:28Z    0.0
29948  2022-11-03T09:53:58Z    0.0

these are the last 5 rows from .cvs file. I need these type of rows deleted

This is my code now:

# libraries
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
import seaborn as sns
from datetime import datetime
import plotly.express as px
from sklearn.ensemble import IsolationForest
import plotly.graph_objects as go

headers = ['time', 'value']

# Read data
#dataset = pd.read_csv("raw_input_filtered.csv", parse_dates=[0])
dataset2 = pd.read_csv("coffe-2col.csv", sep=';', skiprows=1, names=headers) #, dtype=dtypes)

# Printing head of the DataFrame
print(dataset2.head())

# remove empty  lines
#groups = dataset2.groupby((dataset2.drop('value', axis= 1) == 0).all(axis=1))
#all_zero = groups.get_group(True)
#non_all_zero = groups.get_group(False)
new_dataset2 = dataset2.dropna()
#dataset2 = dataset2.drop(dataset2[dataset2.value == 0].index)
#dataset2.drop(dataset2[dataset2.value == 0].index, inplace=True)

non of these worked




CodePudding user response:

# select rows that has value 0
# then take negation of it and return the DF using LOC

df=df.loc[~df['value'].eq(0)]

CodePudding user response:

This would return all rows that do not have a 0 in any of your columns.

dataset2.loc[~(dataset2==0).all(axis=1)]

Alternatively you could replace 'COLUMN' with your column name to only examine that specific column.

dataset2 = dataset2['VALUE'].loc[~(dataset2['VALUE']==0).all(axis=1)]

  • Related