我正在处理来自http://archive.ics.uci的ecoli数据集。edu/ml/数据集/大肠杆菌。这些值由制表符分隔。我想索引每一列,并给他们一个名字。但是,当我使用以下代码执行此操作时:
import pandas as pd
ecoli_cols= ['N_ecoli', 'info1', 'info2', 'info3', 'info4','info5','info6,'info7','type']
d= pd.read_table('ecoli.csv',sep= ' ',header = None, names= ecoli_cols)它不是为每个索引创建名称,而是创建了6个新列。但是,我希望为我已经拥有的每个列设置这些索引名。稍后,我想从这个数据集中提取信息。因此,把它们作为逗号分开或放在表中是很重要的。谢谢
发布于 2017-11-09 11:16:46
您可以在数据和分隔符url中使用\s+ -一个或多个空白空间:
url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/ecoli/ecoli.data'
ecoli_cols= ['N_ecoli', 'info1', 'info2', 'info3', 'info4','info5','info6','info7','type']
df = pd.read_table(url,sep= '\s+',header = None, names= ecoli_cols)
#alternative use parameter delim_whitespace
#df = pd.read_table(url, delim_whitespace= True, header = None, names = ecoli_cols)
print (df.head())
N_ecoli info1 info2 info3 info4 info5 info6 info7 type
0 AAT_ECOLI 0.49 0.29 0.48 0.5 0.56 0.24 0.35 cp
1 ACEA_ECOLI 0.07 0.40 0.48 0.5 0.54 0.35 0.44 cp
2 ACEK_ECOLI 0.56 0.40 0.48 0.5 0.49 0.37 0.46 cp
3 ACKA_ECOLI 0.59 0.49 0.48 0.5 0.52 0.45 0.36 cp
4 ADI_ECOLI 0.23 0.32 0.48 0.5 0.55 0.25 0.35 cp但是,如果要使用带分隔符的文件作为选项卡:
d = pd.read_table('ecoli.csv', sep='\t',header = None, names= ecoli_cols)如果分隔符是;
d = pd.read_table('ecoli.csv', sep=';',header = None, names= ecoli_cols)https://stackoverflow.com/questions/47200280
复制相似问题