删除熊猫数据框中带有零的行

时间:2020-11-10 12:35:53

标签: python pandas dataframe

我要删除的行中的列值等于零。我已经尝试过的东西:

方法1:

df =pd.read_csv('Test.txt', sep="\t", skiprows=10,header=None, 
                                names=["Gewicht", "Temperaturlinks", 
                                "Temperaturrechts",'Raumtemperatur'])
df =df[df['Gewicht'] !=0]
df

方法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)

方法3:

IgnoriereAnfangswerte == 0
TabelleNull= IgnoriereAnfangswerte[~(IgnoriereAnfangswerte == 0).any(axis=1)]
TabelleNull

´´´ 我的数据框如下所示:

Gewicht     a           b           c  

0 24,534000 217,140000 219,970000 27,300000

1 0,000000 217,350000 220,110000 27,300000

2 个答案:

答案 0 :(得分:0)

您的方法1看起来不错。 也尝试:

df = df.drop(df[df['Gewicht']==0].index)

答案 1 :(得分:0)

您的问题的一部分是,似乎每一栏中都有成对的数字。在Python中,这些是tuples

这是在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'])

打印输出:

        Gewicht Temperaturlinks Temperaturrechts Raumtemperatur
0  (24, 534000)   (217, 140000)    (219, 970000)   (27, 300000)
1        (0, 0)   (217, 350000)    (220, 110000)   (27, 300000)

在熊猫中,您drop行符合特定条件。您可以通过以下方法Gewicht(0, 0)删除行:

df[df['Gewicht'] != (0,0) ]

这将打印出来:

        Gewicht Temperaturlinks Temperaturrechts Raumtemperatur
0  (24, 534000)   (217, 140000)    (219, 970000)   (27, 300000)