'How to drop all values that are 0 in a single column pandas dataframe?
I have a pandas DataFrame that was created from some raw data, there are hundreds of lines so I will just show the first 10 rows.
-
text 0 0 1 0 2 0 3 0 4 26.529 5 0 6 25.558 7 0 8 0 9 0 I want to get rid of all the zeros in my data frame and replace the column name from 'text' to 'Results', so the final data should look like this:
-
Results 0 26.529 1 25.558 My method was to use the df.drop() method to drop all rows containing zeros. My code looks like this:
df = df.drop(df[df['text'] == 0].index,inplace=True) # I didn't write the code to replace to column name yetSomehow when I run this, the resulting df is empty/ nonetype. I have no idea why the drop method just dropped everything in my dataframe. Please help! Much appreciated in advance!
When I debug the code in debug mode (vs code), I see the values in my df are as follows:
I noticed that every element in my df is an object type. I want to get rid of all the arrays with an empty object. Ex. "000:array([''],dtype=object)" [1]: https://i.stack.imgur.com/yk63P.png
Solution 1:[1]
You can do that with the following
df[df["text"].str.strip()!="0"].rename(columns={'text':'Results'}).reset_index(drop=True)
Solution 2:[2]
I found a solution for this problem:
First I converted the data type from object to float64 in my df:
df['text'] = pd.to_numeric(df['text'])
Then I proceeded to drop the 'nan' values from the df using:
df = df.dropna()
This works for me!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | CYU1 |
