pandas - apply replace function with condition row-wise

Fabio Lamanna

Starting with this dataframe df:

     0     1     2
02  en    it  None
03  en  None  None
01  nl    en   fil

There are some missing values. I'm trying to apply a replace function row-wise, e.g. in pseudocode:

def replace(x):
    if 'fil' and 'nl' in row:
        x = ''

I know that I can do someting like:

df.apply(f, axis=1)

with a function f defined like:

def f(x):
    if x[0] == 'nl' and x[2] == 'fil':
        x[0] = ''
    return x

obtaining:

     0     1     2
02  en    it  None
03  en  None  None
01        en   fil

but a priori I don't know the actual positions of the strings through the columns, so I have to search with something like the isin method, but row-wise.

EDIT: every string can appear anywhere throughout the columns.

tmthydvnprt

Boolean Indexing and Text Comparison in Pandas

You could create boolean indexing based on string comparisons like this

df['0'].str.contains('nl') & df['2'].str.contains('fil')

or since you updated that the columns could change:

df.isin(['fil']).any(axis=1) & df.isin(['nl']).any(axis=1)

Here is the test case:

import pandas as pd
from cStringIO import StringIO

text_file = '''
     0     1     2
02  en    it  None
03  en  None  None
01  nl    en   fil
'''

# Read in tabular data
df = pd.read_table(StringIO(text_file), sep='\s+')
print 'Original Data:'
print df
print

# Create boolean index based on text comparison
boolIndx = df.isin(['nl']).any(axis=1) & df.isin(['fil']).any(axis=1)
print 'Example Boolean index:'
print boolIndx
print

# Replace string based on boolean assignment   
df.loc[boolIndx] = df.loc[boolIndx].replace('nl', '')
print 'Filtered Data:'
print df
print

Original Data:
    0     1     2
2  en    it  None
3  en  None  None
1  nl    en   fil

Example Boolean index:
2    False
3    False
1     True
dtype: bool

Filtered Data:
    0     1     2
2  en    it  None
3  en  None  None
1        en   fil

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Apply if-else function row-wise

From Java

pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

From Dev

Apply function on each row (row-wise) of a NumPy array

From Dev

Apply a function row-wise to a data.table

From Dev

Column wise mean with condition pandas

From Java

getting the index of a row in a pandas apply function

From Dev

Apply a share function on row pandas DF

From Dev

Pandas: How to structure row-wise apply which requires previous output as input

From Dev

Pandas DataFrame row wise comparison

From Dev

Pandas DataFrame row wise comparison

From Dev

Skip a row when condition is not met using pandas apply

From Dev

PySpark Row-wise Function

From Dev

Apply numpy nonzero row-wise?

From Dev

Use pandas dataframe apply to replace row values from a numpy array

From Dev

How to update table by applying row wise condition

From Dev

Pandas - Apply function and generate more than one row with lambda function

From Dev

Replace value using previous row depending on condition (using a function such as sapply)

From Dev

How do you check a condition of several pandas DataFrame.Series element-wise and apply the result to a new column?

From Dev

How do you check a condition of several pandas DataFrame.Series element-wise and apply the result to a new column?

From Dev

Apply function to pandas dataframe row using values in other rows

From Dev

Getting previous row values from within pandas apply() function

From Dev

Pandas function: DataFrame.apply() runs top row twice

From Dev

Pandas dataframe generate column with different row info, but no apply function

From Dev

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

From Dev

Getting previous row values from within pandas apply() function

From Dev

Apply condition to whole row with dplyr

From Dev

Replace apply function with lapply

From Dev

Replace apply function with lapply

Related Related

  1. 1

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

  2. 2

    Apply if-else function row-wise

  3. 3

    pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

  4. 4

    Apply function on each row (row-wise) of a NumPy array

  5. 5

    Apply a function row-wise to a data.table

  6. 6

    Column wise mean with condition pandas

  7. 7

    getting the index of a row in a pandas apply function

  8. 8

    Apply a share function on row pandas DF

  9. 9

    Pandas: How to structure row-wise apply which requires previous output as input

  10. 10

    Pandas DataFrame row wise comparison

  11. 11

    Pandas DataFrame row wise comparison

  12. 12

    Skip a row when condition is not met using pandas apply

  13. 13

    PySpark Row-wise Function

  14. 14

    Apply numpy nonzero row-wise?

  15. 15

    Use pandas dataframe apply to replace row values from a numpy array

  16. 16

    How to update table by applying row wise condition

  17. 17

    Pandas - Apply function and generate more than one row with lambda function

  18. 18

    Replace value using previous row depending on condition (using a function such as sapply)

  19. 19

    How do you check a condition of several pandas DataFrame.Series element-wise and apply the result to a new column?

  20. 20

    How do you check a condition of several pandas DataFrame.Series element-wise and apply the result to a new column?

  21. 21

    Apply function to pandas dataframe row using values in other rows

  22. 22

    Getting previous row values from within pandas apply() function

  23. 23

    Pandas function: DataFrame.apply() runs top row twice

  24. 24

    Pandas dataframe generate column with different row info, but no apply function

  25. 25

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

  26. 26

    Getting previous row values from within pandas apply() function

  27. 27

    Apply condition to whole row with dplyr

  28. 28

    Replace apply function with lapply

  29. 29

    Replace apply function with lapply

HotTag

Archive