[英]Iterate over columns of Pandas dataframe and create new variables
我無法弄清楚如何迭代 pandas dataframe 中的變量並對每個變量執行相同的算術 function。
我有一個 dataframe df
,其中包含三個數值變量x1
、 x2
和x3
。 我想通過將每個變量乘以 2 來創建三個新變量。這就是我正在做的事情:
existing = ['x1','x2','x3']
new = ['y1','y2','y3']
for i in existing:
for j in new:
df[j] = df[i]*2
上面的代碼實際上是在 dataframe 中創建了三個新變量y1
、 y2
和y3
。 但是y1
和y2
的值被y3
的值覆蓋,並且所有三個變量都具有相同的值,對應於y3
的值。 我不確定我錯過了什么。
非常感謝任何指導/建議。 謝謝。
您在這里循環了 9 次 - 每列 3 次,每次迭代都會覆蓋前一個。
你可能想要類似的東西
for e, n in zip(existing,new):
df[n] = df[e]*2
我會做一些更通用的事情
#existing = ['x1','x2','x3']
exisiting = df.columns
new = existing.replace('x','y')
#maybe you need map+lambda/for for each existing string
for (ind_existing, ind_new) in zip(existing,new):
df[new[ind_new]] = df[existing[ind_existing]]*2
#maybe there is more elegant way by using pandas assign function
您可以將原始 DataFrame 與具有雙倍值的列連接起來:
cols_to_double = ['x0', 'x1', 'x2']
new_cols = list(df.columns) + [c.replace('x', 'y') for c in cols_to_double]
df = pd.concat([df, 2 * df[cols_to_double]], axis=1, copy=True)
df.columns = new_cols
因此,如果您的輸入df
Dataframe 是:
x0 x1 x2 other0 other1
0 0 1 2 3 4
1 0 1 2 3 4
2 0 1 2 3 4
3 0 1 2 3 4
4 0 1 2 3 4
執行前幾行后,您將獲得:
x0 x1 x2 other0 other1 y0 y1 y2
0 0 1 2 3 4 0 2 4
1 0 1 2 3 4 0 2 4
2 0 1 2 3 4 0 2 4
3 0 1 2 3 4 0 2 4
4 0 1 2 3 4 0 2 4
這里是創建df
的代碼:
import pandas as pd
import numpy as np
df = pd.DataFrame(
data=np.column_stack([np.full((5,), i) for i in range(5)]),
columns=[f'x{i}' for i in range(3)] + [f'other{i}' for i in range(2)]
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.