在Python中使用panda将多个堆叠的数据帧重新排列为一个数据帧

2024-05-16 11:09:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python完全陌生,我不确定从哪里开始,尽管熊猫似乎是个不错的选择

我使用一个科学仪器生成以下excel数据文件

https://docs.google.com/spreadsheets/d/1hTRamdpR_GLRvDmyKeamXWL01s57MmamPwFAE_Q7YN8/edit?usp=sharing

下面是数据结构的可视化表示。 https://drive.google.com/file/d/0B36IoWUwP26JNHgtTFNqV3VGZVU/view?usp=sharing

我得到的数据的结构用一个表示(来自图像链接)。我想将excel文件导入python,然后对其重新排序,以给出图B所示的结构

我不知道如何开始这样做,所以任何指针是非常感谢


Tags: httpscomdocs数据文件google结构exceledit
1条回答
网友
1楼 · 发布于 2024-05-16 11:09:49

好吧,这是我根据@EdChum的建议给出的答案。我一次导入了所有数据,然后连接了两个数据帧作为示例。下一步是自动识别excel文件中的不同数据帧,而不必手动定义切片

import pandas as pd

df = pd.read_excel("test data.xlsx",header=None)  #Import all data

df1=df.ix[0:67,:] #Slice first dataframe, give new headings and indices
df1.columns=df1.ix[0,]
df1=df1[1:]
df1=df1.set_index(df1.ix[:,0])

df2=df.ix[69:136,:]  #Slice second dataframe, give new headings and indices
df2.columns=df2.ix[69,]
df2=df2[1:]
df2=df2.set_index(df2.ix[:,0])

frames=[df1,df2] #Define frames as the combination of both databases

result=pd.concat(frames,axis=1) #Concatenate 


result=result.drop('Wavelength', 1) #remove the extra wavelength row

result.to_csv("result.csv") #export result

相关问题 更多 >