[英]iterate though column names of a dataframe to create new dataframe
假設我們有一個 df(100,5)。 所有列都被命名。 例如,我們有以下內容
df1:
A B C D E
1 2 3 4 5
. . . . .
. . . . .
我想遍歷列名並創建一個新的 dataframe,同時調用另一個 function 對單元格中的值進行一些計算。 我有這樣的東西
new_df = []
for name in df:
result = my_function(vector1, df[name])
new_df[name] = function2(result)
如果我有這樣的,我會收到以下錯誤
TypeError: list indices must be integers or slices, not list
我也試過這樣
new_df = []
for name in df:
result = my_function(vector1, df[name])
new_df[[name]] = function2(result)
編輯:當我寫這篇文章時,我不小心寫了
for name in new_df:
我想寫的是
for name in df:
認為這就是你想要的。 您正在循環一個空列表。 此外,作為列表的new_df
不能被索引為new_df[name] =
。 您要么需要使用字典,要么只需要 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.