读取具有多级列的文本文件,Python 3.6
我在没有标题的文本文件中有以下内容读取具有多级列的文本文件,Python 3.6,python,python-3.x,pandas,csv,dataframe,Python,Python 3.x,Pandas,Csv,Dataframe,我在没有标题的文本文件中有以下内容 "Africa","10000" "Brazil","5432" "India","7687" "US","87686","Import" "Itally","2322","Export" 我需要将上面的数据导入3字段数据框,我尝试了下面的脚本,但失败了 Data = pd.read_csv(FilePath, header=None, usecols=['One', 'Two', 'Three'], encoding='ISO-8859-1', dtyp
"Africa","10000"
"Brazil","5432"
"India","7687"
"US","87686","Import"
"Itally","2322","Export"
我需要将上面的数据导入3字段数据框,我尝试了下面的脚本,但失败了
Data = pd.read_csv(FilePath, header=None, usecols=['One', 'Two', 'Three'], encoding='ISO-8859-1', dtype=object)
ValueError: Usecols do not match columns, columns expected but not found: ['Two', 'Three', 'One']
在中使用参数
names
来指定新列名称:
Data = pd.read_csv(FilePath,
header=None,
names=['One', 'Two', 'Three'],
encoding='ISO-8859-1',
dtype=object)
测试:
import pandas as pd
from io import StringIO
temp='''"Africa","10000"
"Brazil","5432"
"India","7687"
"US","87686","Import"
"Itally","2322","Export"'''
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
Data = pd.read_csv(StringIO(temp),
header=None,
names=['One', 'Two', 'Three'],
encoding='ISO-8859-1',
dtype=object)
print (Data)
One Two Three
0 Africa 10000 NaN
1 Brazil 5432 NaN
2 India 7687 NaN
3 US 87686 Import
4 Itally 2322 Export