"python pandas, a function will be applied to the combinations of the elements in one row based on a condition on the other row" Code Answer

2

update:

in [49]: x = pd.dataframe(np.triu(squareform(pdist(df[['distance']], my_func))),
    ...:                  columns=df.car.str.split('_').str[0],
    ...:                  index=df.car.str.split('_').str[0]).replace(0, np.nan)
    ...:

in [50]: x[x.apply(lambda col: col.index != col.name)].max(1).max(level=0)
out[50]:
car
bmw     197.0
fiat      nan
ww      221.0
dtype: float64

old answer:

iiuc you can do something like the following:

from scipy.spatial.distance import pdist, squareform

def my_func(x,y):
    return 2*x + 3*y

x = pd.dataframe(
    squareform(pdist(df[['distance']], my_func)),
    columns=df.car.str.split('_').str[0],
    index=df.car.str.split('_').str[0])

it produced:

in [269]: x
out[269]:
car     bmw    bmw    bmw     ww     ww   fiat   fiat
car
bmw     0.0   95.0   86.0   92.0  131.0  119.0  167.0
bmw    95.0    0.0  116.0  122.0  161.0  149.0  197.0
bmw    86.0  116.0    0.0  116.0  155.0  143.0  191.0
ww     92.0  122.0  116.0    0.0  159.0  147.0  195.0
ww    131.0  161.0  155.0  159.0    0.0  173.0  221.0
fiat  119.0  149.0  143.0  147.0  173.0    0.0  213.0
fiat  167.0  197.0  191.0  195.0  221.0  213.0    0.0

exluding the same brand:

in [270]: x.apply(lambda col: col.index != col.name)
out[270]:
car     bmw    bmw    bmw     ww     ww   fiat   fiat
car
bmw   false  false  false   true   true   true   true
bmw   false  false  false   true   true   true   true
bmw   false  false  false   true   true   true   true
ww     true   true   true  false  false   true   true
ww     true   true   true  false  false   true   true
fiat   true   true   true   true   true  false  false
fiat   true   true   true   true   true  false  false

in [273]: x[x.apply(lambda col: col.index != col.name)]
out[273]:
car     bmw    bmw    bmw     ww     ww   fiat   fiat
car
bmw     nan    nan    nan   92.0  131.0  119.0  167.0
bmw     nan    nan    nan  122.0  161.0  149.0  197.0
bmw     nan    nan    nan  116.0  155.0  143.0  191.0
ww     92.0  122.0  116.0    nan    nan  147.0  195.0
ww    131.0  161.0  155.0    nan    nan  173.0  221.0
fiat  119.0  149.0  143.0  147.0  173.0    nan    nan
fiat  167.0  197.0  191.0  195.0  221.0    nan    nan

selecting maximum per row:

in [271]: x[x.apply(lambda col: col.index != col.name)].max(1)
out[271]:
car
bmw     167.0
bmw     197.0
bmw     191.0
ww      195.0
ww      221.0
fiat    173.0
fiat    221.0
dtype: float64

max per brand:

in [276]: x[x.apply(lambda col: col.index != col.name)].max(1).max(level=0)
out[276]:
car
bmw     197.0
fiat    221.0
ww      221.0
dtype: float64
By WEQA HUDSA on August 19 2022

Answers related to “python pandas, a function will be applied to the combinations of the elements in one row based on a condition on the other row”

Only authorized users can answer the Search term. Please sign in first, or register a free account.