导入 pandas 时忽略字符 [英] Ignore character while importing with pandas
本文介绍了导入 pandas 时忽略字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在文档中找不到这样的选项.测量设备会吐出Excel中的所有内容:
I could not find such an option in the documentation. A measuring device spits out everything in Excel:
<> A B C
1
2
3
当我手动删除<>"字符时,一切正常.有没有办法避免这种情况(无需转换为csv)?
When I delete the "<>" characters manually everything works fine. Is there a way to circumvent that (without conversion to csv)?
我这样做:
import pandas as pd
df = pd.read_excel(filename,sheetname,skiprows=0,header=0,index_col=0)
skiprow = 1
不能解决问题,因为熊猫使用第一行作为名称.如果我提供names = list(range(1, 4))
,则第一行数据会丢失.
skiprow = 1
does not do the trick since pandas uses the first row as names. If I supply names = list(range(1, 4))
the first data row is lost.
推荐答案
另一个选择是:
f = open(fname, 'r')
line1 = f.readline()
data1 = pd.read_csv(f, sep='\s+', names=line1.replace(' #', '').split(), dtype=np.float)
您可能有一个不同的分隔符.
You might have a different separator though.
这篇关于导入 pandas 时忽略字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文