I have a dataframe like this:
df = pd.DataFrame(index=['pre1_xyz', 'pre1_foo', 'pre3_bar', 'pre3_foo', 'pre10_foo', 'pre10_bar', 'pre10_xyz'])
to which I want to add a column values
whereby the value is determined based on the prefix of the index of the respective row using a function return_something(pref)
. Right now I implement that as follows:
import pandas as pd
import numpy as np
# this just returns a random value for the sake of simplicity
def return_something(pref):
return np.random.choice(len(pref)+10)
df = pd.DataFrame(index=['pre1_xyz', 'pre1_foo', 'pre3_bar', 'pre3_foo', 'pre10_foo', 'pre10_bar', 'pre10_xyz'])
# get all the unique prefixes
unique_pref = set([pi.partition('_')[0] for pi in df.index])
# determine the value for each prefix
val_pref = {pref: return_something(pref) for pref in unique_pref}
# add the values to the dataframe
for prefi, vali in val_pref.items():
# determine all rows with the same prefix
rows = [rowi for rowi in df.index if rowi.startswith(prefi+'_')]
df.loc[rows, 'values'] = vali
That then gives me the desired output:
values
pre1_xyz 0
pre1_foo 0
pre3_bar 7
pre3_foo 7
pre10_foo 13
pre10_bar 13
pre10_xyz 13
Question is whether there is anything smarter than this e.g. a solution which avoids creating unique_pref
and/or val_pref
and/or makes use of set_value
which seems to be the fastest solution to add values to a dataframe as discussed in this question.