remove 0 values from dataframe 1

remove 0 values from dataframe

df.loc[~(df==0).all(axis=1)]

Here is what the above code is Doing:
1. df.loc[~(df==0).all(axis=1)]
– This is a boolean mask that will return True for all rows that have at least one non-zero value.
– The ~ is a bitwise operator that inverts the True/False values.
– The .all(axis=1) is a boolean method that returns True if all values in a row are True.
– The .loc[] is a pandas method that allows you to select rows and columns by label.
2. df.loc[~(df==0).all(axis=1)] = np.nan
– This replaces all rows that have at least one non-zero value with NaN.

Similar Posts