Home > Blockchain >  Pandas Dataframe: Dropping Selected rows with 0.0 float type values
Pandas Dataframe: Dropping Selected rows with 0.0 float type values

Time:02-20

Please I have a dataset that contains amount as float type. Some of the rows contain values of 0.00 and because they skew the dataset, I need to drop them. I have temporarily set the "Amount" to index and sorted the value as well. Afterwards, I attempted to drop the rows after subsetting with iloc but eep getting error message in the form ValueError: Buffer has wrong number of dimensions (expected 1, got 3)

'''mortgage = mortgage.set_index('Gross Loan Amount').sort_values('Gross Loan Amount') mortgage.drop([mortgage.loc[0.0]])'''

I equally tried this: '''mortgage.drop(mortgage.loc[0.0])''' it flagged the error of the form KeyError: "[Column_names] not found in axis"

Please how else can I accomplish the task?

CodePudding user response:

You could make a boolean frame and then use any

df = df[~(df == 0).any(axis=1)]

in this code, all rows that have at least one zero in their data has been removed

CodePudding user response:

Let me see if I get your problem. I created this sample dataset:

df = pd.DataFrame({'Values': [200.04,100.00,0.00,150.15,69.98,0.10,2.90,34.6,12.6,0.00,0.00]})
df
    Values
0   200.04
1   100.00
2     0.00
3   150.15
4    69.98
5     0.10
6     2.90
7    34.60
8    12.60
9     0.00
10    0.00

Now, in order to get rid of the 0.00 values, you just have to do this:

df = df[df['Values'] != 0.00]

Output:

df
    Values
0   200.04
1   100.00
3   150.15
4    69.98
5     0.10
6     2.90
7    34.60
8    12.60
  • Related