1 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊
這是您問(wèn)題的有效解決方案。我目前的假設(shè)是儀器的采樣率是相同的。由于您沒(méi)有提供任何樣本,所以我生成了一些數(shù)據(jù)。答案基于連接Wavelength列上的兩個(gè)數(shù)據(jù)幀。
import pandas as pd
import numpy as np
##generating the test data
black_lambda = np.arange(300.2,795.5,0.1)
red_lambda = np.arange(199.975,1027.43,0.1)
I_black = np.random.random((1,len(black_lambda))).ravel()
I_red = np.random.random((1,len(red_lambda))).ravel()
df = pd.DataFrame([black_lambda,I_black]).T
df1 = pd.DataFrame([red_lambda,I_red]).T
df.columns=['lambda','I_black']
df1.columns=['lambda','I_red']
從這里開(kāi)始:
#setting lambda as index for both dataframes
df.set_index(['lambda'],inplace=True)
df1.set_index(['lambda'],inplace=True)
#concatenating/merging both dataframes into one
df3 = pd.concat([df,df1],axis=1)
#since both dataframes are not of same length, there will be some missing values. Taking care of them by filling previous values (optional).
df3.fillna(method='bfill',inplace=True)
df3.fillna(method='ffill',inplace=True)
#creating a new column 'division' to finish up the task
df3['division'] = df3['I_black'] / df3['I_red']
print(df3)
輸出:
I_black I_red division
lambda
199.975 0.855777 0.683906 1.251308
200.075 0.855777 0.305783 2.798643
200.175 0.855777 0.497258 1.720993
200.275 0.855777 0.945699 0.904915
200.375 0.855777 0.910735 0.939655
... ... ... ...
1026.975 0.570973 0.637064 0.896258
1027.075 0.570973 0.457862 1.247042
1027.175 0.570973 0.429709 1.328743
1027.275 0.570973 0.564804 1.010924
1027.375 0.570973 0.246437 2.316917
添加回答
舉報(bào)