Home > OS >  How can I take two lists of Pandas DataFrame columns names, and only use one list, but append a stri
How can I take two lists of Pandas DataFrame columns names, and only use one list, but append a stri

Time:08-18

I have two separate DataFrames:

df_a
df_b

The column names, type of data, and length are identical but the numbers are different. I want to subtract (df_a - df_b) and record that number in a new dataframe df_dif. I can manage that okay with what I have below:

colnames_in = ['col a', 'col b', 'col c']
colnames_out = ['new col a', 'new col b', 'new col c']

    for i in range(len(colnames_in)):
        df_dif[colnames_out[i] = df_a[colnames_in[i]] - df_b[colnames_in[i]]

Where I am stuck is trying to be less repetitious. How would i accomplish the same, but instead of listing out colnames_out as a list...just append the string 'new' ?

CodePudding user response:

i think this should work:

df_new = df_a.copy()
df_new.subtract(df_b)

CodePudding user response:

You can do this vectorized using pandas.substract:

df_a = pd.DataFrame({'A':[10, 20, 30], 'B':[40, 50, 60]})
df_b = pd.DataFrame({'A':[1, 2, 3], 'B':[4, 5, 6]})

df_dif = df_a.subtract(df_b)

print(df_dif)

Output:

    A   B
0   9  36
1  18  45
2  27  54
  • Related