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...
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.
Comments