我有一个数据帧df1,它由一个带有零的列和另一个长度相似的数据帧df2组成,但包含一列光谱数据,即峰值。
鉴于我已经在df2中找到了峰值位置,我想在每个峰值上取一个df2切片并在df1中插入相应的值,从而替换为零。
有任何建议怎么做?
由于
答案 0 :(得分:0)
所以我在发布问题后很快就找到了问题的解决方案:
df1["counts"][(peak.index[0]-win_size):(peak.index[0]+win_size)] = df2[(peak.index[0]-win_size):(peak.index[0]+win_size)]
其中df1是由零组成的数据帧(列为“count”),而peak是包含源自df2的峰值位置的数据帧
答案 1 :(得分:0)
import numpy
# load spectrum
df2 = numpy.array([1,2,5,3,1,0.5,0.4,1,2,5,3,0.1,0.3,0.6,1,2,5,1.5,0.6,0.4,0.7,0.9,1,3,5,2.5],dtype = float)
# generate zero array equivalent to spectrum
df1 = numpy.zeros(len(df2),dtype = float)
# you know what is the peak value
peakvalue = 5
selectedpoints = []
# identify the peak point places
for i in range(len(df2)):
if df2[i] == peakvalue:
selectedpoints.append(i)
# how much len to select on each side
selected_len = 1
for i in range(len(selectedpoints)):
df1[selectedpoints[i]-selected_len : selectedpoints[i]+selected_len+1] = df2[selectedpoints[i]-selected_len : selectedpoints[i]+selected_len+1]
print(df1)