这里我只是摆出代码中抛出错误的部分。这里我将合并两个不同的数据帧集,它们附加在两个不同的列表中。在
path1 = '/home/Desktop/computed_2d_blaze/'
path2 = '/home/Desktop/computed_1d/'
path3 = '/home/Desktop/sn_airmass_seeing/'
dir1 = [x for x in os.listdir(path1) if '.ares' in x]
dir2 = [x for x in os.listdir(path2) if '.ares' in x]
dir3 = [x for x in os.listdir(path3) if '.ares' in x]
lst = []
lst1 = []
for file1, file2,file3 in zip(dir1,dir2,dir3):
df1 = pd.read_table(path1+file1, skiprows=0, usecols=(0,1,2,3,4,8),names=['wave','num','stlines','fwhm','EWs','MeasredWave'],delimiter=r'\s+')
df2 = pd.read_table(path2+file2, skiprows=0, usecols=(0,1,2,3,4,8),names=['wave','num','stlines','fwhm','EWs','MeasredWave'],delimiter=r'\s+')
df1 = df1.groupby('wave').mean().reset_index()
df1 = df1.sort_values('wave').reset_index(drop=True)
df2 = df2.sort_values('wave').reset_index(drop=True)
dfs = pd.merge(df1,df2, on='wave', how='inner')
dfs['delta_ew'] = (dfs.EWs_x - dfs.EWs_y)
dfs=dfs.filter(items=['wave','delta_ew'])
lst.append(dfs)
df3 = pd.read_table(path3+file3, skiprows=0, usecols=(0,1,2),names=['seeing','airmass','snr'],delimiter=r'\s+')
lst1.append(df3)
[df.set_index('wave', inplace=True) for df in lst]
df=pd.concat(lst,axis=1,join='inner')
x = pd.concat(lst1,axis=1,join='inner')
for z in df.index:
t = x.loc[0, 'airmass']
s = df.loc[z, 'delta_ew']
dfs = pd.concat([s,t],axis=1,names=['delta_ew','airmass'])
dfs = dfs[np.abs(dfs.delta_ew - dfs.delta_ews.mean()) <= (dfs.delta_ews.mad())]
当我试图创建一个新的数据帧时,因为delta_ew
中有一些异常值,所以为了删除它们,我这样做了。但当我试图这样做时,我得到了一个错误ValueError: cannot reindex from a duplicate axis
。在
我不知道如何解决这个错误。谁能告诉我我哪里出错了吗?在
这是完整的回溯
^{pr2}$
当索引具有重复值时,将联接/分配给列时,此错误通常会增加。在
错误是从
dfs = pd.concat([s,t],axis=1,names=['delta_ew','airmass'])
代码引发的。我相信我找到了解决你问题的办法。只需将ignore_index=True
添加到concat
代码中。在像这样:
这将重新创建索引。在
注:
index
表示行和列名称我终于解决了这个问题。我用了
dictionary
,而不是concat
。因为我面临的问题是合并两个熊猫系列来制作新的数据帧。我首先将pandas系列t & s
的值转换成dictionary,然后将字典转换成一个dataframe,它对我来说工作得非常好。在相关问题 更多 >
编程相关推荐