[英]Delete rows with Zeros in pandas dataframe
I want to delete rows were on of the column values is equal to Zero.我想删除列值等于零的行。 What I allready tried:
我已经尝试过:
Method 1:方法一:
df =pd.read_csv('Test.txt', sep="\t", skiprows=10,header=None,
names=["Gewicht", "Temperaturlinks",
"Temperaturrechts",'Raumtemperatur'])
df =df[df['Gewicht'] !=0]
df
Method 2:方法二:
# Get names of indexes for which column Stock has value No
indexNamesL = IgnoriereAnfangswerte[IgnoriereAnfangswerte["Gewicht"]== 0 ].index
# Delete these row indexes from dataFrame
IgnoriereNullL=IgnoriereAnfangswerte.drop(indexNamesL,inplace=True)
print(IgnoriereNull)
Method 3:方法三:
IgnoriereAnfangswerte == 0
TabelleNull= IgnoriereAnfangswerte[~(IgnoriereAnfangswerte == 0).any(axis=1)]
TabelleNull
´´´ My Dataframe looks like this: ´´´ 我的数据框如下所示:
Gewicht a b c
0 24,534000 217,140000 219,970000 27,300000 0 24,534000 217,140000 219,970000 27,300000
1 0,000000 217,350000 220,110000 27,300000 1 0,000000 217,350000 220,110000 27,300000
your method 1 looks OK.您的方法 1 看起来不错。 also try:
也可以尝试:
df = df.drop(df[df['Gewicht']==0].index)
A part of your problem is that it seems that you have pairs of numbers in each column.您的问题的一部分是每列中似乎都有一对数字。 In Python, these are
tuples
.在 Python 中,这些是
tuples
。
Here is a way to create the dataframe in Python:这是在 Python 中创建数据框的一种方法:
import pandas as pd
rows = [[(24,534000), (217,140000), (219,970000), (27,300000)],
[(0,0), (217,350000), (220,110000), (27,300000)]]
df =pd.DataFrame(data=rows, columns=["Gewicht", "Temperaturlinks", "Temperaturrechts",'Raumtemperatur'])
This prints out:这打印出来:
Gewicht Temperaturlinks Temperaturrechts Raumtemperatur
0 (24, 534000) (217, 140000) (219, 970000) (27, 300000)
1 (0, 0) (217, 350000) (220, 110000) (27, 300000)
In Pandas, you drop
a row meeting a certain criterion.在 Pandas 中,您可以
drop
满足特定条件的行。 Here is how you can drop a row with a Gewicht
of (0, 0)
:以下是如何删除
Gewicht
为(0, 0)
:
df[df['Gewicht'] != (0,0) ]
This will print out:这将打印出:
Gewicht Temperaturlinks Temperaturrechts Raumtemperatur
0 (24, 534000) (217, 140000) (219, 970000) (27, 300000)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.