How to use pandas apply function on all columns of some rows of data frame

NG_21

I have a dataframe. I want to replace values of all columns of some rows to a default value. Is there a way to do this via pandas apply function

Here is the dataframe

import pandas as pd
temp=pd.DataFrame({'a':[1,2,3,4,5,6],'b':[2,3,4,5,6,7],'c':['p','q','r','s','t','u']})
mylist=['p','t']

How to replace values in columns a and bto default value 0,where value of column c is in mylist

Is there a way to do this using pandas functionality,avoiding for loops

EdChum

Use isin to create a boolean mask and use loc to set the rows that meet the condition to the desired new value:

In [37]:
temp.loc[temp['c'].isin(mylist),['a','b']] = 0
temp

Out[37]:
   a  b  c
0  0  0  p
1  2  3  q
2  3  4  r
3  4  5  s
4  0  0  t
5  6  7  u

result of the inner isin:

In [38]:
temp['c'].isin(mylist)

Out[38]:
0     True
1    False
2    False
3    False
4     True
5    False
Name: c, dtype: bool

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use pandas apply function on all columns of some rows of data frame

From Dev

how to apply duplicated function to the columns of a data frame

From Dev

How to apply scipy function on Pandas data frame

From Dev

How to apply a function to all fields (all columns of all rows)

From Dev

How can I use if else to change values in some rows and columns in my data frame in R?

From Dev

Apply function row wise on pandas data frame on columns with numerical values

From Dev

Use an apply function to a subset of rows in a data frame - vectorised solution

From Dev

Deleting rows in pandas data frame after evaluating all columns

From Dev

Getting the names of all the columns and number of rows in a data frame with Python Pandas

From Dev

Deleting rows in pandas data frame after evaluating all columns

From Dev

How to apply functions with multiple arguments on Pandas selected columns data frame

From Dev

Apply function to heterogeneous rows of data.frame

From Dev

How to properly apply a lambda function into a pandas data frame column

From Dev

How to apply custom function to pandas data frame for each row

From Dev

R Apply function on data frame columns

From Dev

pandas apply function to multiple columns and multiple rows

From Dev

How to apply a function that works with data.frame cells to data.frame columns

From Dev

How to apply a function that works with data.frame cells to data.frame columns

From Dev

how to get unique values in all columns in pandas data frame

From Dev

R: pass function with multiple parameters to data frame rows, using only some columns

From Dev

Group by and apply custom function in pandas data frame

From Dev

How to use vectorized function to check across columns in data frame

From Java

Drop rows with all zeros in pandas data frame

From Dev

using t.test with apply on some columns of a data frame

From Dev

Not able to view all columns in Pandas Data frame

From Dev

Function for all rows in a data frame R

From Dev

Apply LOESS filter / regression to all columns of my data frame

From Dev

How do I apply a function in R to certain columns of a data frame grouped by another column?

From Dev

Apply function in data frame

Related Related

  1. 1

    How to use pandas apply function on all columns of some rows of data frame

  2. 2

    how to apply duplicated function to the columns of a data frame

  3. 3

    How to apply scipy function on Pandas data frame

  4. 4

    How to apply a function to all fields (all columns of all rows)

  5. 5

    How can I use if else to change values in some rows and columns in my data frame in R?

  6. 6

    Apply function row wise on pandas data frame on columns with numerical values

  7. 7

    Use an apply function to a subset of rows in a data frame - vectorised solution

  8. 8

    Deleting rows in pandas data frame after evaluating all columns

  9. 9

    Getting the names of all the columns and number of rows in a data frame with Python Pandas

  10. 10

    Deleting rows in pandas data frame after evaluating all columns

  11. 11

    How to apply functions with multiple arguments on Pandas selected columns data frame

  12. 12

    Apply function to heterogeneous rows of data.frame

  13. 13

    How to properly apply a lambda function into a pandas data frame column

  14. 14

    How to apply custom function to pandas data frame for each row

  15. 15

    R Apply function on data frame columns

  16. 16

    pandas apply function to multiple columns and multiple rows

  17. 17

    How to apply a function that works with data.frame cells to data.frame columns

  18. 18

    How to apply a function that works with data.frame cells to data.frame columns

  19. 19

    how to get unique values in all columns in pandas data frame

  20. 20

    R: pass function with multiple parameters to data frame rows, using only some columns

  21. 21

    Group by and apply custom function in pandas data frame

  22. 22

    How to use vectorized function to check across columns in data frame

  23. 23

    Drop rows with all zeros in pandas data frame

  24. 24

    using t.test with apply on some columns of a data frame

  25. 25

    Not able to view all columns in Pandas Data frame

  26. 26

    Function for all rows in a data frame R

  27. 27

    Apply LOESS filter / regression to all columns of my data frame

  28. 28

    How do I apply a function in R to certain columns of a data frame grouped by another column?

  29. 29

    Apply function in data frame

HotTag

Archive