Reputation: 159
Imagine that we have a df(100,5). All columns are named. Let s say for example we have the following
df1:
A B C D E
1 2 3 4 5
. . . . .
. . . . .
I want to loop through the column names and create a new dataframe while calling another function that does some calculations with the values in the cells. I have something like this
new_df = []
for name in df:
result = my_function(vector1, df[name])
new_df[name] = function2(result)
If I have it like this, I get the following error
TypeError: list indices must be integers or slices, not list
I have also tried like this
new_df = []
for name in df:
result = my_function(vector1, df[name])
new_df[[name]] = function2(result)
EDIT : When I was writing the post I accidentally wrote
for name in new_df:
What I meant to write was
for name in df:
Upvotes: 0
Views: 237
Reputation: 552
Think this is what you want. You were looping through an empty list.
Also, a new_df
as a list can't be indexed as new_df[name] =
. You'd either need to use a dictionary or just append.
new_df = []
for name in df.columns:
result = my_function(vector1, df[name])
new_df.append(function2(result))
new_df = pd.DataFrame(new_df, columns = df.columns)
Upvotes: 2