简体   繁体   English

为什么我的 python 只读取 CSV 文件中的 1 列?

[英]Why my python only read only 1 column from a CSV file?

Recently, I tried to analyze some csv files, but when I tried to read the csv file into a dataframe, I found that the dataframe had only one column, and the csv file obviously had several columns. Recently, I tried to analyze some csv files, but when I tried to read the csv file into a dataframe, I found that the dataframe had only one column, and the csv file obviously had several columns. The csv file is a test record from a test machine,if I open it with notebook,it looks like this: csv文件是一个测试机的测试记录,如果我用笔记本打开,是这样的:

在此处输入图像描述


If I open it in excel, it looks like this(over 300 rows):如果我用 excel 打开它,它看起来像这样(超过 300 行):在此处输入图像描述


What I want to do is to take a specific part of the whole csv file(from row 323 to row 327) for analysis , this part looks like this:我要做的是对整个 csv 文件(从第 323 行到第 327 行)的特定部分进行分析,这部分如下所示:

在此处输入图像描述

I've tried:我试过了:

df = pd.read_csv(filepath, sep=';', header=None,)  #read csv with pandas
df.dropna(axis=0, how='all', inplace=True)  #delet empty rows
df = df.iloc[215:300]   #take the data I need from dataframe

But when I ran df.info(), I got this error:但是当我运行 df.info() 时,我得到了这个错误:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 85 entries, 215 to 299
Data columns (total 1 columns):
0    85 non-null object
dtypes: object(1)
memory usage: 1.3+ KB

There was only 1 column, and what I need was a tabular form with separated columns(like it shows in Excel) so that I could calculate those numbers.只有一列,我需要的是一个带有分隔列的表格形式(就像它在 Excel 中显示的那样),以便我可以计算这些数字。 I googled this problem and stuggled a few days,no matter what I did, I could only get 1 column.我google了这个问题,纠结了几天,不管怎么做,都只能得到1列。 Really need some help or guidance here,thanks in advance.在这里真的需要一些帮助或指导,在此先感谢。

It is a data-cleaning process, you can read the file in one column, then extract the target rows, then split and expand it.这是一个数据清理过程,您可以在一列中读取文件,然后提取目标行,然后拆分和展开它。

# same code, just change sep='\n'
df = pd.read_csv(filepath, sep='\n', header=None,)  #read csv with pandas
df.dropna(axis=0, how='all', inplace=True)  #delet empty rows
df = df.iloc[215:300]   #take the data I need from dataframe

# split and expand(new)
dfn = df[0].str.split(',', expand=True)
# then save it as a new file
dfn.to_csv('new_file.csv', index=False, header=None)
# read it again
df = pd.read_csv('new_file.csv')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM