What's a better way to use df.apply on multiple columns?

Celso Pereira Neto

After struggling with a csv file encoding I decided to do the encoding heresy of manually replacing some characters.

This is how the dataframe looks:

df = pd.DataFrame({'a' : 'bÉd encoded',
               'b' : ['foo', 'bar'] * 3,
               'c' : 'bÉd encoded too'})


              a    b                 c
0  bÉd encoded  foo  bÉd encoded too
1  bÉd encoded  bar  bÉd encoded too
2  bÉd encoded  foo  bÉd encoded too
3  bÉd encoded  bar  bÉd encoded too
4  bÉd encoded  foo  bÉd encoded too
5  bÉd encoded  bar  bÉd encoded too

If my only problem was column 'a' this function would be enough:

def force_good_e(row):
    col = row['a']
    if 'É' in col:
        col = col.replace('É','a') 
    return col

df['a'] = df.apply(force_good_e, axis=1)

But then I would need another function for column 'c'

I got an improvement with this:

def force_good_es(row, column):
    col = row[column]
    if 'É' in col:
        col = col.replace('É','a') 
    return col


df['a'] = df.apply(lambda x: force_good_es(x,'a'), axis=1)
df['c'] = df.apply(lambda x: force_good_es(x,'c'), axis=1)

But it got me wondering, is there a better way to do this?

i.e. eliminating the need to make one line of

df[n] = df.apply(lambda x: force_good_es(x,n), axis=1)

for each n column that needs to be fixed.

Abhi

You could use str.replace

df['a'] = df['a'].str.replace('É','a')
df['c'] = df['c'].str.replace('É','a')

or like @wen mentioned in comments.

df = df.replace({'É':'a'},regex=True)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Apply Search to Multiple Columns

分類Dev

Apply (in Pandas) to Multiple Columns

分類Dev

mutating multiple columns automatically in a df

分類Dev

Apply over xts with multiple columns

分類Dev

What is a better way to write this SQL query?

分類Dev

What is the better way for passing props to component in React?

分類Dev

What is a better way to write an if statement using PHP

分類Dev

Better way of capturing multiple same tags?

分類Dev

Better way to write Case When Multiple conditions

分類Dev

Better way to summarize multiple groups in same dataframe

分類Dev

Is there a better way to replace multiple spaces in file names?

分類Dev

Better way to check for multiple regex conditions in Ruby?

分類Dev

Angular - What's the best way to include html in multiple components?

分類Dev

pandas multiply multiple columns to make new df

分類Dev

Is there a better way to re-use plots Matplotlib?

分類Dev

Pandas DataFrame apply function to multiple columns and output multiple columns

分類Dev

What's the recommended way to use/install webapps on Arch Linux?

分類Dev

pass output from pandas apply to multiple columns

分類Dev

apply calculation on multiple columns of each element in a list

分類Dev

What is the better way to use IP field (IPV4 or IPV6) in proto3 file for Golang and C# usage

分類Dev

What is better practice: to store a string or use relations?

分類Dev

Better to use 1 or multiple database tables?

分類Dev

What is the best way to conditionally apply attributes in AngularJS?

分類Dev

Process multiple columns with Pandas apply method when columns are not predefined

分類Dev

What is the better way of show a Modal Form with a dim background?

分類Dev

What is better way for merge sort? recursive function or non-recursive?

分類Dev

What is the right way to use "instanceof"?

分類Dev

Better way to use findstr to get packet loss, batch

分類Dev

What's a good way to take multiple parallel subsets of a data frame in R?

Related 関連記事

  1. 1

    Apply Search to Multiple Columns

  2. 2

    Apply (in Pandas) to Multiple Columns

  3. 3

    mutating multiple columns automatically in a df

  4. 4

    Apply over xts with multiple columns

  5. 5

    What is a better way to write this SQL query?

  6. 6

    What is the better way for passing props to component in React?

  7. 7

    What is a better way to write an if statement using PHP

  8. 8

    Better way of capturing multiple same tags?

  9. 9

    Better way to write Case When Multiple conditions

  10. 10

    Better way to summarize multiple groups in same dataframe

  11. 11

    Is there a better way to replace multiple spaces in file names?

  12. 12

    Better way to check for multiple regex conditions in Ruby?

  13. 13

    Angular - What's the best way to include html in multiple components?

  14. 14

    pandas multiply multiple columns to make new df

  15. 15

    Is there a better way to re-use plots Matplotlib?

  16. 16

    Pandas DataFrame apply function to multiple columns and output multiple columns

  17. 17

    What's the recommended way to use/install webapps on Arch Linux?

  18. 18

    pass output from pandas apply to multiple columns

  19. 19

    apply calculation on multiple columns of each element in a list

  20. 20

    What is the better way to use IP field (IPV4 or IPV6) in proto3 file for Golang and C# usage

  21. 21

    What is better practice: to store a string or use relations?

  22. 22

    Better to use 1 or multiple database tables?

  23. 23

    What is the best way to conditionally apply attributes in AngularJS?

  24. 24

    Process multiple columns with Pandas apply method when columns are not predefined

  25. 25

    What is the better way of show a Modal Form with a dim background?

  26. 26

    What is better way for merge sort? recursive function or non-recursive?

  27. 27

    What is the right way to use "instanceof"?

  28. 28

    Better way to use findstr to get packet loss, batch

  29. 29

    What's a good way to take multiple parallel subsets of a data frame in R?

ホットタグ

アーカイブ