I would like to divide all columns, except the first, with a specific column of a dataframe and add the results as new columns with a new header, but I'm stuck. Here is my approach, but please be gentle, I just started programming a month ago..:
I got this example dataframe:
np.random.seed(0)
data = pd.DataFrame(np.random.randint(1,10,size=(100, 10)),
columns=list('ABCDEFGHIJ'))
Now I create a list of the columns and drop 'A' and 'J':
cols = list(data.drop(columns=['A', 'J']).columns)
Then I would like to divide the columns B-I by column J. In this example this would be easy, since there are just single letters, but the column names are longer in reality (for example "Donaudampfschifffahrtkapitän" (there are really funny and long words in german). That's why I want to do it with the "cols"-list.
data[[cols]] = data[[cols]].div(data['J'].values,axis=0)
However, I get this error:
KeyError: "None of [Index([('B', 'C', 'D', 'E', 'F', 'G', 'H', 'I')], dtype='object')] are in the [columns]"
What is wrong? Or does someone knows an even better approach?
And how can I add the results with their specific names ('B/J', 'C/J', ..., 'I/J') to the dataframe?
Thx in advance!