0

I have a dataframe containing incalculable rows and columns. The df is structured in such that until 6th row and 2nd column, I have string as input and the rest are numbers(floating points). I want to discard the rows from the df whose sum is equal to zero, however, keeping the structure intact. I don't want to drop the first 6 rows and the 1st column as they hold crucial dataframe.

Original dataframe(df): original_df

Expected output: expected_df2

I tried this way but it didn't do the expected job. Instead the 6 rows and the 1st column have been removed removed.

df_filt = df.iloc[6:, 1:].astype(float)

rows_filt = df_filt.sum(axis=1) > 0

if rows_filt.any(): df = df.loc[rows_filt.index] else: print("No row sum equals zero.")

3
  • if is just 0, you can do df[~df.eq(0).all(axis=1)] Commented Jan 24 at 16:48
  • @Triky that worked, but the indices and the column headers are gone! Commented Jan 24 at 17:19
  • I would separate the origina df in 2, make the neeed operations on the 2nd datafame (all rows except the first 6) and then put them back together with pd.concat(). Commented Jan 26 at 9:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.