简体   繁体   English

PANDAS从不存在的txt文件中读取多余的列

[英]PANDAS reading extra column from txt file that doesn't exist

Im trying to read a txt into python using pd.read.csv where the columns are separated by tabs. 我试图使用pd.read.csv将txt读取到python中,其中的列由制表符分隔。 I want to read it as a csv. 我想将其阅读为csv。 When it reads the file it adds an extra column with no header full of NAN entries. 读取文件时,它会添加一个额外的列,其中没有包含NAN条目的标题。 How do I avoid it reading this extra non-existing column. 如何避免读取此额外的不存在的列。 Code looks like this: 代码如下:

data = pd.read_csv('Desktop/SciKit Projects/Task 1/Datasets/Skin_NonSkin.txt', sep = '\t')

DataSet looks like this: DataSet看起来像这样:

 B    G   R  Target  
 74  85  123      1  
 73  84  122      1  
 75  81  112      1  
 ...

and it ends up being as read as this: 它最终被读为:

       B     G    R  Target  Unnamed: 4
 0     74   85  123       1         NaN
 1     73   84  122       1         NaN
 2     72   83  121       1         NaN
 3     70   81  119       1         NaN

Thank you 谢谢

尝试这个:

pd.read_csv(filename, delim_whitespace=True, skipinitialspace=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM