[英]python pandas: try to read txt file but showed NaN
I have a txt file and I want to read it with pandas, 我有一个txt文件,我想和熊猫一起阅读,
and I wrote: 我写道:
#!/usr/bin/python
import pandas as pd
import numpy as np
TC=pd.read_csv('D885_Ch10_ZC.csv',error_bad_lines=False,encoding='gbk')
df=pd.DataFrame(TC,columns=['t[s]','digits[]','Ch10_zc[V]'])
print(df)
and I found the data was replace by NaN which I don't know why. 我发现数据被NaN取代了,我不知道为什么。
What goes wrong? 怎么了?
thx @jezrael 's answer. thx @jezrael的答案。 It works after I deleted all the useless information on the top.
在删除顶部所有无用的信息后,此方法将起作用。 Is there anyway to do this without editing the original file?
无论如何,不编辑原始文件就可以这样做吗?
I think you need sep
parameter, because default is sep=','
. 我认为您需要
sep
参数,因为默认值为sep=','
。
if tab
: 如果
tab
:
names=['t[s]','digits[]','Ch10_zc[V]']
df=pd.read_csv('D885_Ch10_ZC.csv',
sep='\t',
error_bad_lines=False,
encoding='gbk',
names=names,
skiprows=1 )
if whitespaces: 如果空格:
names=['t[s]','digits[]','Ch10_zc[V]']
df=pd.read_csv('D885_Ch10_ZC.csv',
sep='\s+',
encoding='gbk',
error_bad_lines=False,
names=names,
skiprows=1)
names=['t[s]','digits[]','Ch10_zc[V]']
df=pd.read_csv('D885_Ch10_ZC.csv',
delim_whitespace=True,
encoding='gbk',
error_bad_lines=False,
names=names,
skiprows=1)
and if 2 or more whitespaces: 并且如果2个或更多的空格:
names=['t[s]','digits[]','Ch10_zc[V]']
df=pd.read_csv('D885_Ch10_ZC.csv',
sep=r'\s{2,}',
engine='python',
encoding='gbk',
names=names,
skiprows=1 )
EDIT: 编辑:
Need change skiprows to 10
: 需要将增加的行数更改为
10
:
names=['t[s]','digits[]','Ch10_zc[V]']
df=pd.read_csv(StringIO(temp),
delim_whitespace=True,
encoding='gbk',
names=names,
skiprows=10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.