英文:
How do I make a horizontal data list print into a series of columns?
问题
我有一个数据列表,看起来像这样:日期=27-06-2023;时间=12:16:15.8650000;TraceP...
我如何将其拆分为具有日期、时间等标题的单独列?
例如:日期 时间
27-06-2023 12:16:15.8650000
我正在使用pandas。
到目前为止,我只是这样打印它:
N=[]
for f in csv_files:
# 读取csv文件
rf = pd.read_csv(f)
N.append(rf) # 保存在大文件中
# 删除最后几行
rf.drop(rf.tail(16385).index,
inplace = True)
# 打印位置和文件名
# print('Location:', f)
print('File Name:', f.split("\\")[-1])
# 打印内容
print('Content:')
display(rf)
print()
在column_subset=[日期,时间,TracePoints,TSamp,TimeUnits,AmpToVolts,TraceMaxVolts,PTime,STime]
usecols=column_subset
N
英文:
I have a data list that looks like this: Date=27-06-2023; Time=12:16:15.8650000; TraceP...
How do I make that into separate columns with Date, Time, etc. as the titles?
Ex. Date Time
27-06-2023 12:16:15.8650000
I am using pandas.
So far I am just printing it like this:
N=[]
for f in csv_files:
# read the csv file
rf = pd.read_csv(f)
N.append(rf) #save in the massive file
# drop last few rows
rf.drop(rf.tail(16385).index,
inplace = True)
# print the location and filename
# print('Location:', f)
print('File Name:', f.split("\\")[-1])
# print the content
print('Content:')
display(rf)
print()
In column_subset=['Date', 'Time', 'TracePoints', 'TSamp', 'TimeUnits', 'AmpToVolts', 'TraceMaxVolts', 'PTime', 'STime']
usecols=column_subset
N
Out[ ATF v1.00
0 Date=27-06-2023; Time=14:46:24.1960000; TraceP...,
ATF v1.00
0 Date=27-06-2023; Time=14:46:24.1960000; TraceP...,
ATF v1.00
0 Date=27-06-2023; Time=14:46:24.1960000; TraceP...,
ATF v1.00
0 Date=27-06-2023; Time=14:46:24.1960000; TraceP...,
答案1
得分: 1
根据你的问题,看起来你的CSV文件格式不正确。因此,pd.read_csv() 很可能不能直接使用。通常在CSV文件中,列应该由逗号分隔,但在你的情况下,它们是由分号分隔的,列名与数据本身一起包含在内。
这个问题的一个可能解决方案是首先将你的数据作为普通文本文件读取,解析每一行以在分号处拆分值,然后从每个拆分部分提取列名和数据。以下是一个可能有效的示例:
import pandas as pd
N = []
for f in csv_files:
# 以文本文件方式读取文件
with open(f, 'r') as file:
data = []
for line in file:
# 在分号处拆分行
parts = line.split(";")
row = {}
for part in parts:
# 在等号处拆分部分
name_value = part.split("=")
if len(name_value) == 2:
# 该部分包含列名和值
name = name_value[0].strip()
value = name_value[1].strip()
row[name] = value
data.append(row)
# 将数据转换为DataFrame
df = pd.DataFrame(data)
N.append(df)
希望这能帮助你处理CSV文件中的数据。
英文:
From your question, it looks like your data in the CSV file is not properly formatted. Hence pd.read_csv() will most likely not work out of the box. Normally in a CSV file, the columns should be separated by commas, but in your case, they are separated by semicolons and the column names are included with the data itself.
A possible solution to this is to read your data as a simple text file first, parse each line to split the values at the semicolons, then extract the column names and data from each split part. Here's an example which may work.
import pandas as pd
N = []
for f in csv_files:
# read the file as a text file
with open(f, 'r') as file:
data = []
for line in file:
# split the line at semicolons
parts = line.split(";")
row = {}
for part in parts:
# split the part at the equals sign
name_value = part.split("=")
if len(name_value) == 2:
# the part is a column name and value
name = name_value[0].strip()
value = name_value[1].strip()
row[name] = value
data.append(row)
# convert the data to a DataFrame
df = pd.DataFrame(data)
N.append(df)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论