您可以将参数usecols与列顺序一起使用:
import pandas as pdfrom pandas.compat import StringIOtemp=u"""TIME XGSM2004 006 01 00 01 37 600 12004 006 01 00 02 32 800 52004 006 01 00 03 28 000 82004 006 01 00 04 23 200 112004 006 01 00 05 18 400 17"""#after testing replace StringIO(temp) to filenamedf = pd.read_csv(StringIO(temp), sep="s+", skiprows=1, usecols=[0,7], names=['TIME','XGSM'])print (df) TIME XGSM0 2004 11 2004 52 2004 83 2004 114 2004 17
编辑:
您可以使用分隔符
regex-2个或更多空格,然后添加,
engine='python'因为出现警告:
ParserWarning:回退到“ python”引擎,因为“ c”引擎不支持正则表达式分隔符(分隔符> 1个字符且与“ s
+”不同的分隔符被解释为正则表达式);您可以通过指定engine =’python’来避免此警告。
import pandas as pdfrom pandas.compat import StringIOtemp=u"""TIME XGSM2004 006 01 00 01 37 600 12004 006 01 00 02 32 800 52004 006 01 00 03 28 000 82004 006 01 00 04 23 200 112004 006 01 00 05 18 400 17"""#after testing replace StringIO(temp) to filenamedf = pd.read_csv(StringIO(temp), sep=r's{2,}', engine='python')print (df) TIME XGSM0 2004 006 01 00 01 37 600 11 2004 006 01 00 02 32 800 52 2004 006 01 00 03 28 000 83 2004 006 01 00 04 23 200 114 2004 006 01 00 05 18 400 17


