繁体   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. csv文件是一个测试机的测试记录,如果我用笔记本打开,是这样的:

在此处输入图像描述


如果我用 excel 打开它,它看起来像这样(超过 300 行):在此处输入图像描述


我要做的是对整个 csv 文件(从第 323 行到第 327 行)的特定部分进行分析,这部分如下所示:

在此处输入图像描述

我试过了:

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

但是当我运行 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

只有一列,我需要的是一个带有分隔列的表格形式(就像它在 Excel 中显示的那样),以便我可以计算这些数字。 我google了这个问题,纠结了几天,不管怎么做,都只能得到1列。 在这里真的需要一些帮助或指导,在此先感谢。

这是一个数据清理过程,您可以在一列中读取文件,然后提取目标行,然后拆分和展开它。

# 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