很抱歉标题可能比问题本身更复杂;(
我已经取消了对熊猫数据帧的跟踪
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…的新列。。。。取决于变量anc的值。例如,在第一行,anc=5,因此lap1应等于anc5(0.1500(的值,lap2等于anc6
因此,输出应该看起来像
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
设置grh
&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
&CCD_ 6恢复为列。
替代基于名称的查找而非位置查找:
定义一个实用函数来执行提供浮点值的列查找。它需要接受float,因为如果该系列包含任何非整数值,panda会自动将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)
有点"蛮力"的方法,但我看不出你怎么能做到这一点:
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(
# 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
让我知道,如果这给你提供某种解决方案正在寻找