简体   繁体   中英

iterate though column names of a dataframe to create new dataframe

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM