Pandas Data Frame function application to a specific row

Michal

I'm trying to apply one function f1 to rows ['Utah,'Texas'] and f2 to other rows. I don't want to create separate DF for each function.

Example adjusted from Wes McKinney's Python for Data Analysis:

MWE:

import pandas as pd
import numpy as np

frame = pd.DataFrame(np.random.randn(4, 3), 
                  columns=list('bde'), 
                  index=['Utah', 'Ohio', 'Texas', 'Oregon'])

f1 = lambda x: (x-x.min())/(x.max() - x.min())
f2 = lambda x: (x-x.max())/(x.min() - x.max())

I've tried selecting row by label: frame.loc['Utah'].apply(f1,axis=1).

I can feel that it is something very small that I'm missing but...

Ami Tavory

This creates a 2d numpy.array whose each row is the application of one of the two functions to the dataframe according to the rules you specified for that row:

np.where(
    np.array([frame.index.isin(['Utah', 'Texas']) for _ in frame.columns]).T,
    frame.apply(f1, axis=1),
    frame.apply(f2, axis=1))

Since you didn't specify the output fully, it's hard to guess what you want to do further.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Function for specific data frame

From Dev

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

From Dev

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

From Dev

Applying function to each row of pandas data frame - with speed

From Dev

Delete specific row in a data frame?

From Dev

Calculating CAGR by row by row in a pandas data frame?

From Dev

Segmenting data frame by row based on specific pattern

From Dev

How to look for a value in a specific pandas data frame column and then store the other values from that row in separate variables

From Dev

Counting a row of pandas data frame in another data frame

From Dev

pandas: Add row from one data frame to another data frame?

From Dev

Access Pandas Data Frame row with index value

From Dev

python: changing row index of pandas data frame

From Dev

remove punctuation for each row in a pandas data frame

From Dev

Moving desired row to the top of pandas Data Frame

From Dev

Pandas data frame: replace nan with the mean of that row

From Dev

Adding a column to certain row in a data frame pandas

From Dev

Plot a function for every row in an R data frame

From Dev

Combine a specific row from many data frames into one data frame

From Dev

Reserializing row index after row selection in pandas data frame

From Dev

How to apply scipy function on Pandas data frame

From Dev

Cumulative Sum Function on Pandas Data Frame

From Dev

Filtering pandas data frame with tabulated function

From Dev

Group by and apply custom function in pandas data frame

From Dev

Transforming pandas data frame using stack function

From Dev

Pandas: join a row of data on specific index number

From Dev

How to remove a specific row number from a data frame in r

From Dev

Row-wise sort then concatenate across specific columns of data frame

From Dev

Count consecutive occurrences of a specific value in every row of a data frame in R

From Dev

Change values in data frame in a specific row using dplyr

Related Related

  1. 1

    Function for specific data frame

  2. 2

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

  3. 3

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

  4. 4

    Applying function to each row of pandas data frame - with speed

  5. 5

    Delete specific row in a data frame?

  6. 6

    Calculating CAGR by row by row in a pandas data frame?

  7. 7

    Segmenting data frame by row based on specific pattern

  8. 8

    How to look for a value in a specific pandas data frame column and then store the other values from that row in separate variables

  9. 9

    Counting a row of pandas data frame in another data frame

  10. 10

    pandas: Add row from one data frame to another data frame?

  11. 11

    Access Pandas Data Frame row with index value

  12. 12

    python: changing row index of pandas data frame

  13. 13

    remove punctuation for each row in a pandas data frame

  14. 14

    Moving desired row to the top of pandas Data Frame

  15. 15

    Pandas data frame: replace nan with the mean of that row

  16. 16

    Adding a column to certain row in a data frame pandas

  17. 17

    Plot a function for every row in an R data frame

  18. 18

    Combine a specific row from many data frames into one data frame

  19. 19

    Reserializing row index after row selection in pandas data frame

  20. 20

    How to apply scipy function on Pandas data frame

  21. 21

    Cumulative Sum Function on Pandas Data Frame

  22. 22

    Filtering pandas data frame with tabulated function

  23. 23

    Group by and apply custom function in pandas data frame

  24. 24

    Transforming pandas data frame using stack function

  25. 25

    Pandas: join a row of data on specific index number

  26. 26

    How to remove a specific row number from a data frame in r

  27. 27

    Row-wise sort then concatenate across specific columns of data frame

  28. 28

    Count consecutive occurrences of a specific value in every row of a data frame in R

  29. 29

    Change values in data frame in a specific row using dplyr

HotTag

Archive