基于不同的其他列,根据另一列的值新建pandas列。

问题描述 投票:0回答:1

对不起,标题可能比问题本身更复杂;)

我有以下的pandas数据框架。

    grh  anc     anc1     anc2    anc3     anc4     anc5    anc6     anc7  
1     2    5  0.10000  0.12000  0.1800  0.14000  0.15000  0.1900  0.20000   
2     3    7  0.03299  0.05081  0.0355  0.02884  0.03054  0.0332  0.03115   
3     4    3  0.00000  0.00000  0.0000  0.00000  0.00000  0.0000  0.00000   
4     5    4  0.00000  0.00000  0.0000  0.00000  0.00000  0.0000  0.00000   
5     6    1  0.10000  0.10000  0.1000  0.10000  0.10000  0.1000  0.10000   


       anc8     anc9    anc10  
1   0.10000  0.21000  0.24000  
2   0.02177  0.04903  0.04399  
3   0.00000  0.00000  0.00000  
4   0.00000  0.00000  0.00000  
5   0.10000  0.10000  0.10000  

我想用forloop添加新的列,lap1, lap2, ...取决于变量anz的值。例如,在第一行,anz=5,所以lap1应该等于anz5的值。(0.1500),lap2等于祖先6 (0.1900)...在第二排第一圈=anc7。(0.03115),lap2=anc8 (0.02177),...

所以,输出应该是这样的

grh anc anc1    anc2    anc3    anc4    anc5    anc6    anc7    anc8    anc9    anc10   lap1    lap2    lap3
2   5   0.10000 0.12000 0.18000 0.14000 0.15000 0.19000 0.20000 0.1000  0.21000 0.24000 0.15000 0.19000 0.20000
3   7   0.03299 0.05081 0.0355  0.02884 0.03054 0.0332  0.03115 0.02177 0.04903 0.04399 0.03115 0.02177 0.04903
4   3   0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
5   4   0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
6   1   0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000

我试过一些很基本的东西,但似乎并不奏效。

for i in range(1,4):
    j=df['anc']+i
    df['lap'+str(i)]= df['anc'+str(j)]

如果你有任何想法,我将非常感激.Thks

pandas lookup calculated-columns
1个回答
0
投票

有点像 "蛮力 "的方法,但我看不出你有什么别的办法。

df[[f"lap{i}" for i in range(1,4)]]= \
    df.apply(lambda x: \
        pd.Series({f"lap{j}": x[f"anc{int(j+x['anc']-1)}"] for j in range(1,4)}) \
    , axis=1)

(假设根据你的样本,你有最大的数量) lap (3)


1
投票

设置 grh &amp。anc 作为索引,因为我们正在寻找索引到的。anc[1-9] 列。这在我们写输出列的时候也很方便。

df2 = df.set_index(['grh', 'anc']) 

对于每一行都要使用 anc 值,也就是现在的索引中的值,取相邻的3个值,将它们转换为输出中所期望的名称的系列,并将它们分配到匹配的输出列上

outcols = ['lap1', 'lap2', 'lap3']
df2[outcols] = df2.apply(lambda x: pd.Series(x[x.name[1]-1:x.name[1]+2].values, index=outcols), axis=1)

df2是这样的。

            anc1     anc2    anc3     anc4     anc5    anc6     anc7     anc8     anc9    anc10     lap1     lap2     lap3
grh anc
2   5    0.10000  0.12000  0.1800  0.14000  0.15000  0.1900  0.20000  0.10000  0.21000  0.24000  0.15000  0.19000  0.20000
3   7    0.03299  0.05081  0.0355  0.02884  0.03054  0.0332  0.03115  0.02177  0.04903  0.04399  0.03115  0.02177  0.04903
4   3    0.00000  0.00000  0.0000  0.00000  0.00000  0.0000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
5   4    0.00000  0.00000  0.0000  0.00000  0.00000  0.0000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
6   1    0.10000  0.10000  0.1000  0.10000  0.10000  0.1000  0.10000  0.10000  0.10000  0.10000  0.10000  0.10000  0.10000

如果你想恢复的话,再重新设置一次索引。grh &amp。anc 恢复为列。


替代基于名称的查找,而不是基于位置的查找。

定义一个实用函数来执行列查找,提供一个float。它需要接受一个float,因为如果系列包含任何非整数值,pandas会自动将int64上传到float64。使用这个函数来执行查找&来分配输出。这种方法的一个好处是,没有 set_index 是必须的。

def cols(n,p): return [f'{p}{i}' for i in range(int(n), int(n+3))] 
df[cols(1, 'lap')] = df.apply(lambda x: pd.Series(x[cols(x.anc, 'anc')].values), axis=1)

0
投票
# Where is the new lap column starting
startingNewColsNumber  = df.shape[1]

# How many new lap columns to add
numNewCols = df.grh.max() 

# Generate new lap columns
newColNames = ['lap'+str(x) for x in range(1, numNewCols + 1)]

# add new lap columns to the dataframe
for lapName in newColNames:
    df[lapName] = np.NaN

# now fill the values for each of rows for the new 'lap' columns 
for row in df.index:
    startCopyCol = df.loc[row,'anc'] + 1   # What is the begening anc value to start copying
    howmany = df.loc[row,'grh']            # How many lap values should I fill
    df.iloc[row, startingNewColsNumber : startingNewColsNumber + howmany]  =  \
    df.iloc[row, startCopyCol : startCopyCol + howmany].values

df 

这是我得到的输出。

grh anc anc1    anc2    anc3    anc4    anc5    anc6    anc7    anc8    anc9    anc10   lap1    lap2    lap3    lap4    lap5    lap6
0   2   5   0.10000 0.12000 0.1800  0.14000 0.15000 0.1900  0.20000 0.10000 0.21000 0.24000 0.15000 0.19000 NaN NaN NaN NaN
1   3   7   0.03299 0.05081 0.0355  0.02884 0.03054 0.0332  0.03115 0.02177 0.04903 0.04399 0.03115 0.02177 0.04903 NaN NaN NaN
2   4   3   0.00000 0.00000 0.0000  0.00000 0.00000 0.0000  0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.0 NaN NaN
3   5   4   0.00000 0.00000 0.0000  0.00000 0.00000 0.0000  0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.0 0.0 NaN
4   6   1   0.10000 0.10000 0.1000  0.10000 0.10000 0.1000  0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.10000 0.1 0.1 0.1

如果这能给你提供一些解决方案,请告诉我

© www.soinside.com 2019 - 2024. All rights reserved.