假设您的 csv 设置如下:
Name,Header-1,Header-2,Header-3
Random,Note, ,
Jack,X,X,
Jane,X, ,
,,,
Name,Header-3,Header-2,Header-1
Random,note, ,
Jeremy,X,X,
Joey, , ,X
您可以使用以下不言自明的代码处理此文件:
import pandas as pd
# Read csv file
df = pd.read_csv("D:/tmp/data.csv", sep=',')
#Find columns which are null, create partitions and group by them
isnull = (df["Name"].isnull())
partitions = (isnull != isnull.shift()).cumsum()
gb = df[~isnull].groupby(partitions)
keys = gb.groups.keys()
# Extract all the dataframes
dfs = [gb.get_group(g) for g in keys]
datas = []
# Set the header as first row for all dataframes that are not the first one
for i,data in enumerate(dfs):
if i!=0: # First dataframe has already set the correct header
data.columns = data.ix[data.index[0]]
data = data.drop(data.index[0])
datas.append(data)
# Concatenate the dataframes and reset the index
df_concat = pd.concat(datas)
df_out = df_concat.reset_index(drop=True)
# Change the order of the columns to get "Name" as first column
cols = df_out.columns.tolist()
cols = cols[-1:] + cols[:-1]
df_out = df_out[cols]
所以你的输入是:
>>> df
Name Header-1 Header-2 Header-3
0 Random Note
1 Jack X X
2 Jane X
3 NaN NaN NaN NaN
4 Name Header-3 Header-2 Header-1
5 Random note
6 Jeremy X X
7 Joey X
请注意,在此示例中,标头在要提取的第二个数据帧中的顺序不同。
你的输出将是:
>>> df_out
Name Header-1 Header-2 Header-3
0 Random Note
1 Jack X X
2 Jane X
3 Random note
4 Jeremy X X
5 Joey X