-1

Consider I have a dataframe that looks like this:

   A  B   C   D
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

What I need to do is to sum the C and D and if the sum is higher than 10 remove the entire row. Howerver I can't acess the columns by their names, I need to do it by their position.

How can I do it in pandas?

EDIT: Another problem.

   A  B   C   D
0  0  NaN   2   3
1  4  5   NaN   NaN
2  8  9  10  11

How can I keep the rows that have at least two values in the columns B, C and D?

Lucas Lazari
  • 119
  • 9
  • 3
    Does this answer your question? [How to select rows from a DataFrame based on column values?](https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values) – IoaTzimas Oct 02 '20 at 19:03

3 Answers3

1

Why can't you access the columns by names? If you really can't, you can do this:

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'A': [0, 4, 8], 'B': [1, 5, 9], 'C': [2, 6, 10], 'D': [3, 7, 11]})

In [3]: df
Out[3]:
   A  B   C   D
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

In [4]: df.loc[df.iloc[:, 2:].sum(axis=1) <= 10]
Out[4]:
   A  B  C  D
0  0  1  2  3
paxton4416
  • 495
  • 3
  • 7
1

To keep the rows that have at least two values in the columns B, C and D. You can use this.

df = pd.DataFrame({'A': [0,4,8], 'B':[1, np.nan, 9], 'C':[2,np.nan, np.nan], 'D':[3, 7, 11]})
mask = df.iloc[:,1:].isnull().sum(axis=1) < 2
print(df[mask])

output

    A   B   C   D
0   0   1.0 2.0 3
2   8   9.0 NaN 11

For your first question you should fill remaining nan values using df.fillna(), Documentation before moving with solution provided in earlier answers

df = new_df.fillna(0)
print(df)

Output

   A    B    C   D
0  0  1.0  2.0   3
2  8  9.0  0.0  11

Now you can use df.loc[df.iloc[:, 2:].sum(axis=1) <= 10] to drop the rows where the sum of C and D is more than 10.

Finally I recommend you to keep 1 post per question, this helps other people to search for it.

Ajay Verma
  • 610
  • 2
  • 12
0

This is:

df[(df['C']+df['D']) <= 10]
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74