Hello, I have obtained a data frame through the previous processing, and I would like to remove the column with all the values of 0 and create a new data frame.
For example,
df = pd.DataFrame([[0.0, 0.0224, 0.0123, 0.0],
[0.0, 0.0, 0.0145, 0.0],
[0.0, 0.0145, 0.0000, 0.0],
[0.0, 0.0105, 0.0255 ,0.0]],
columns = [a, b, c, d])
If the data frame was created in this way, I would like to delete columns a and d.
The actual data frame is much larger, so I think it's a lot bigger
In this process, I wonder how to list and extract only the columns with df.count() == 0
separately. I've tried many things, but I'm not sure.
Is there a more convenient way other than this?
Please Thank you.
dataframe python pandas
>>> df = pd.DataFrame([[0.0, 0.0224, 0.0123, 0.0],
[0.0, 0.0, 0.0145, 0.0],
[0.0, 0.0145, 0.0000, 0.0],
[0.0, 0.0105, 0.0255 ,0.0]],
columns = list("abcd"))
>>> df
a b c d
0 0.0 0.0224 0.0123 0.0
1 0.0 0.0000 0.0145 0.0
2 0.0 0.0145 0.0000 0.0
3 0.0 0.0105 0.0255 0.0
>>> df.a == 0.0
0 True
1 True
2 True
3 True
Name: a, dtype: bool
>>> all(df.a == 0.0)
True
>>> all(df.d == 0.0)
True
>>> all(df.b == 0.0)
False
### Add to comment questions.
>>> for col in df.columns:
if all(df[col] == 0.0):
print(values in the f"{col} column are all 0")
All values in the column a are zero
All values in the d column are zero
© 2024 OneMinuteCode. All rights reserved.