Home > Software engineering >  Pandas loop into variables adding suffix and transforming original column
Pandas loop into variables adding suffix and transforming original column

Time:04-13

I would like to loop into some variable name and the equivalent column with an added suffix "_plus"

#original dataset
raw_data = {'time': [2,1,4,2],
            'zone': [5,1,3,0],
           'time_plus': [5,6,2,3],
            'zone_plus': [0,9,6,5]}
df = pd.DataFrame(raw_data, columns = ['time','zone','time_plus','zone_plus'])
df

#desired dataset
df['time']=df['time']*df['time_plus']
df['zone']=df['zone']*df['zone_plus']
df

I would like to do the multiplication in a more elegant way, through a loop, since I have many variables with this pattern: original name * transformed variable with the _plus suffix

something similar to this or better


my_list=['time','zone']

for i in my_list:
    df[i]=df[i]*df[i "_plus"]

CodePudding user response:

raw_data = {'time': [2,1,4,2],
        'zone': [5,1,3,0],
       'time_plus': [5,6,2,3],
        'zone_plus': [0,9,6,5]}
df = pd.DataFrame(raw_data, columns = ['time','zone','time_plus','zone_plus'])

# Take every column that doesn't have a "_plus" suffix
cols = [i for i in list(df.columns) if "_plus" not in i]

# Calculate new columns
for col in cols:
    df[str(col "_2")] = df[col]*df[str(col "_plus")]

I decided to create the new columns with a "_2" suffix, this way we don't mess up the original data.

CodePudding user response:

Try:

for c in df.filter(regex=r".*(?<!_plus)$", axis=1):
    df[c] *= df[c   "_plus"]

print(df)

Prints:

   time  zone  time_plus  zone_plus
0    10     0          5          0
1     6     9          6          9
2     8    18          2          6
3     6     0          3          5

Or:

for c in df.columns:
    if not c.endswith("_plus"):
        df[c] *= df[c   "_plus"]

CodePudding user response:

for c in df.columns:
    if f"{c}_plus" in df.columns:
        df[c] *= df[f"{c}_plus"]

  • Related