简体   繁体   English

遍历 dataframe 的列名以创建新的 dataframe

[英]iterate though column names of a dataframe to create new dataframe

Imagine that we have a df(100,5).假设我们有一个 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.我想遍历列名并创建一个新的 dataframe,同时调用另一个 function 对单元格中的值进行一些计算。 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] = .此外,作为列表的new_df不能被索引为new_df[name] = You'd either need to use a dictionary or just append.您要么需要使用字典,要么只需要 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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