Python 从其他列中的值获取列索引nr
我对python和pandas相对来说比较陌生,因此我可能不完全了解所有的可能性,并希望得到如何解决以下问题的提示: 我有一个像这样的Python 从其他列中的值获取列索引nr,python,pandas,numpy,dataframe,Python,Pandas,Numpy,Dataframe,我对python和pandas相对来说比较陌生,因此我可能不完全了解所有的可能性,并希望得到如何解决以下问题的提示: 我有一个像这样的df: Jan Feb Mar Apr i j a 100 200 250 100 1 0.3 b 120 130 90 100 3 0.7 c 10 30 10 20 2 0.25 我想构造一个列,该列根据df['I']获取带有索引的列,然后将所选列中的值与df['j']中的值相乘。 我想
df
:
Jan Feb Mar Apr i j
a 100 200 250 100 1 0.3
b 120 130 90 100 3 0.7
c 10 30 10 20 2 0.25
我想构造一个列,该列根据df['I']
获取带有索引的列,然后将所选列中的值与df['j']
中的值相乘。
我想创建这样一个表(df['k']
beeing the column constructed):
(行a
df['k']=200*0.3
(df['Feb']*df['j']
),行b
df['k']=100*0.7
(df['Apr']*df['j']
)和行c
df['k']=10*0.25(df['Mar']
df['i']
中的值始终是一个整数值,因此我希望根据df['i']
中的值使用列的位置,然后我们可以使用map
。最后我们使用
输出
Jan Feb Mar Apr i j k
a 100 200 250 100 1 0.30 60.0
b 120 130 90 100 3 0.70 70.0
c 10 30 10 20 2 0.25 2.5
备选方案:
df['j'].mul(df.iloc[:,df['i']].lookup(df.index,
df['i'].map(dict(zip(range(len(df.columns)),
df.columns)))))
我觉得一定有更好的方法,但您可以像这样使用itertuples
:
list_k=[]
对于df.itertuples()中的行:
月份=行[int(行[5]+1)]#元组索引
j=第[6]行
清单k.追加(月*j)
df['k']=列表
提供的解决方案的另一个选项:
#convert column i to a list
vals = df.i.tolist()
#get the number indices for the dataframe
num_indices = [df.index.get_loc(ind) for ind in df.index]
# or df.index.get_indexer(df.index)
#create a pair of the indices and vals
paired = list(zip(num_indices,vals))
#calculate column k by multiplying each extract with column j
df['k'] = [df.iloc[entry] for entry in paired] * df.j
Jan Feb Mar Apr i j k
a 100 200 250 100 1 0.30 60.0
b 120 130 90 100 3 0.70 70.0
c 10 30 10 20 2 0.25 2.5
更新:@ansev是正确的,不需要循环:
#get the column labels that correspond with the values in column i:
col_labels = df.columns[df.i]
#get the values from each column using pandas' lookup:
result = df.lookup(df.index, col_labels)
#multiply the array with column j:
df['k'] = result * df.j
#u can compress this in one line, but i believe breaking it down
#allows for readability :
#df = df.assign(k = df.lookup(df.index,df.columns[df.i])*df.j)
Jan Feb Mar Apr i j k
a 100 200 250 100 1 0.30 60.0
b 120 130 90 100 3 0.70 70.0
c 10 30 10 20 2 0.25 2.5
在这里使用循环是不必要的,它是缓慢的,在这里使用循环是不必要的,它是缓慢的,需要快速响应!这个解决方案对我很有效!
#convert column i to a list
vals = df.i.tolist()
#get the number indices for the dataframe
num_indices = [df.index.get_loc(ind) for ind in df.index]
# or df.index.get_indexer(df.index)
#create a pair of the indices and vals
paired = list(zip(num_indices,vals))
#calculate column k by multiplying each extract with column j
df['k'] = [df.iloc[entry] for entry in paired] * df.j
Jan Feb Mar Apr i j k
a 100 200 250 100 1 0.30 60.0
b 120 130 90 100 3 0.70 70.0
c 10 30 10 20 2 0.25 2.5
#get the column labels that correspond with the values in column i:
col_labels = df.columns[df.i]
#get the values from each column using pandas' lookup:
result = df.lookup(df.index, col_labels)
#multiply the array with column j:
df['k'] = result * df.j
#u can compress this in one line, but i believe breaking it down
#allows for readability :
#df = df.assign(k = df.lookup(df.index,df.columns[df.i])*df.j)
Jan Feb Mar Apr i j k
a 100 200 250 100 1 0.30 60.0
b 120 130 90 100 3 0.70 70.0
c 10 30 10 20 2 0.25 2.5