问题描述
我想使用熊猫读取和处理csv文件。该文件(如下所示)包含多个标头行,这些行由#
标记指示。我可以使用
import pandas as pd
file = "data.csv"
data = pd.read_csv(file,delimiter="\s+",names=["Time","Cd","Cs","Cl","CmRoll","CmPitch","CmYaw","Cd(f)","Cd(r)","Cs(f)","Cs(r)","Cl(f)","Cl(r)"],skiprows=13)
但是,我有很多这样的文件具有不同的标题名称,并且我不想手动命名它们(Time Cd Cs ...
)。每个文件之间的注释行数也不同。所以我想自动化该任务。
在将数据传递到pandas数据框之前,是否需要在此处使用正则表达式之类的内容?
谢谢您的建议。
是的,标头名称也以#
开头。
data.csv:
# Force coefficients
# dragDir : (9.9735673312816520e-01 7.2660490528994301e-02 0.0000000000000000e+00)
# sideDir : (0.0000000000000000e+00 0.0000000000000000e+00 -1.0000000000000002e+00)
# liftDir : (-7.2660490528994315e-02 9.9735673312816520e-01 0.0000000000000000e+00)
# rollAxis : (9.9735673312816520e-01 7.2660490528994301e-02 0.0000000000000000e+00)
# pitchAxis : (0.0000000000000000e+00 0.0000000000000000e+00 -1.0000000000000002e+00)
# yawAxis : (-7.2660490528994315e-02 9.9735673312816520e-01 0.0000000000000000e+00)
# magUInf : 4.5000000000000000e+01
# lRef : 5.9399999999999997e-01
# Aref : 3.5639999999999999e-03
# CofR : (1.4999999999999999e-01 0.0000000000000000e+00 0.0000000000000000e+00)
#
# Time Cd Cs Cl CmRoll CmPitch CmYaw Cd(f) Cd(r) Cs(f) Cs(r) Cl(f) Cl(r)
5e-06 1.8990180226147195e+00 1.4919925634649792e-11 2.1950119509976829e+00 -1.1085971520784955e-02 -1.0863798447281650e+00 9.5910040927874810e-03 9.3842303978657482e-01 9.6059498282814471e-01 9.5910041002474442e-03 -9.5910040853275178e-03 1.1126130770676479e-02 2.1838858202270064e+00
1e-05 2.1428508927716594e+00 1.0045114197556737e-08 2.5051633252700962e+00 -1.2652317494411272e-02 -1.2367567798452046e+00 1.0822379290263353e-02 1.0587731288914184e+00 1.0840777638802410e+00 1.0822384312820453e-02 -1.0822374267706254e-02 1.5824882789843508e-02 2.4893384424802525e+00
...
解决方法
在读取文件之前提取标头怎么样?
我们仅假设您的标题行以#
开头。标头及其在文件中的位置的提取是自动进行的。我们还确保不会读取多余的行(第一条数据行除外)。
with open(file) as f:
line = f.readline()
cnt = 0
while line.startswith('#'):
prev_line = line
line = f.readline()
cnt += 1
# print(prev_line)
header = prev_line.strip().lstrip('# ').split()
df = pd.read_csv(file,delimiter="\s+",names=header,skiprows=cnt
)
这样,您还可以处理其他标题行。它还提供了标题在文件中的位置。
,这应该做到,它既简单又高效,它将变量减到最少,并且除了文件名外不需要任何输入。
with open(file,'r') as f:
for line in f:
if line.startswith('#'):
header = line
else:
break #stop when there are no more #
header = header[1:].strip().split()
data = pd.read_csv(file,comment='#',names=header)
您首先打开文件,并仅读取注释行(它将快速且节省内存)。最后一个有效行将是最终标头,该标头将被清除并转换为列表。最后,使用pandas.read_csv()
和comment='#'
打开文件,这将跳过注释行和names=header
。
一些正则表达式可能会有所帮助。
这不是最漂亮的解决方案,请随时发布更好的解决方案。
让我们读取任何文件的前50行,以查找哈希的最后一次出现,该哈希应该是列名。
-
^声明行首的位置
-
#
从字面上匹配字符#(区分大小写)
import re
n_rows = 50
path_ = 'your_file_location'
with open(path_,'r') as f:
data = []
for i in range(n_rows): # read only 50 rows here.
for line in f:
if re.match('^#',line):
data.append(line)
start_col = max(enumerate(data))[0]
df = pd.read_csv(path_,sep='\s+',skiprows=start_col) # use your actual delimiter.
# Time Cd Cs Cl CmRoll CmPitch \
0 0.000005 1.899018 1.491993e-11 2.195012 -0.011086 -1.086380 0.009591
1 0.000010 2.142851 1.004511e-08 2.505163 -0.012652 -1.236757 0.010822
CmYaw Cd(f) Cd(r) Cs(f) Cs(r) Cl(f) Cl(r)
0 0.938423 0.960595 0.009591 -0.009591 0.011126 2.183886 NaN
1 1.058773 1.084078 0.010822 -0.010822 0.015825 2.489338 NaN
编辑,处理列名中的#
。
我们可以分两个步骤进行操作。
我们可以读取0行,但可以切片标题列。
首先从标题行读取文件,但将header
参数设置为None
,这样就不会设置标题。
然后我们可以手动设置列标题。
df = pd.read_csv(path_,skiprows=start_col + 1,header=None)
df.columns = pd.read_csv(path_,skiprows=start_col,nrows=0).columns[1:]
print(df)
Time Cd Cs Cl CmRoll CmPitch CmYaw \
0 0.000005 1.899018 1.491993e-11 2.195012 -0.011086 -1.086380 0.009591
1 0.000010 2.142851 1.004511e-08 2.505163 -0.012652 -1.236757 0.010822
Cd(f) Cd(r) Cs(f) Cs(r) Cl(f) Cl(r)
0 0.938423 0.960595 0.009591 -0.009591 0.011126 2.183886
1 1.058773 1.084078 0.010822 -0.010822 0.015825 2.489338
,
为简化它并节省时间而不使用循环,您可以为#
带注释的行创建2个数据框,其余的则创建。
从这些注释行中移出最后一个-这是您的标题,然后使用concat()
合并数据数据框和此标题,如果需要将第一行分配为标题,则可以使用 df.columns=df.iloc[0]
df = pd.DataFrame({
'A':['#test1 : (000000)','#test1 (000000)','#Time (000000)','5e-06','1e-05'],})
print(df)
A
0 #test1 : (000000)
1 #test1 (000000)
2 #test1 (000000)
3 #test1 (000000)
4 #Time (000000)
5 5e-06
6 1e-05
df_header = df[df.A.str.contains('^#')]
print(df_header)
A
0 #test1 : (000000)
1 #test1 (000000)
2 #test1 (000000)
3 #test1 (000000)
4 #Time (000000)
df_data = df[~df.A.str.contains('^#')]
print(df_data)
A
5 5e-06
6 1e-05
df = (pd.concat([df_header.iloc[[-1]],df_data])).reset_index(drop=True)
df.A=df.A.str.replace(r'^#',"")
print(df)
A
0 Time (000000)
1 5e-06
2 1e-05
,
假定注释始终以单个“#”开头,并且标题位于最后注释的行中:
import csv
def read_comments(csv_file):
for row in csv_file:
if row[0] == '#':
yield row.split('#')[1].strip()
def get_last_commented_line(filename):
with open(filename,'r',newline='') as f:
decommented_lines = [line for line in csv.reader(read_comments(f))]
header = decommented_lines[-1]
skiprows = len(decommented_lines)
return header,skiprows
header,skiprows = get_last_commented_line(path)
pd.read_csv(path,skiprows=skiprows)