gpt4 book ai didi

python - PANDAS 从不存在的 txt 文件中读取额外的列

转载 作者:太空宇宙 更新时间:2023-11-04 00:27:37 25 4
gpt4 key购买 nike

我正在尝试使用 pd.read.csv 将 txt 读入 python,其中列由制表符分隔。我想将其作为 csv 阅读。当它读取文件时,它会添加一个额外的列,其中没有充满 NAN 条目的标题。我如何避免它阅读这个额外的不存在的专栏。代码如下所示:

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

数据集看起来像这样:

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

它最终被读成这样:

       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

谢谢

最佳答案

试试这个:

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

关于python - PANDAS 从不存在的 txt 文件中读取额外的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46921008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com