报错解决(3):AttributeError: '_io.TextIOWrapper' object has no attribute 'head'. Did you mean: 'read'?

问题描述:

在pycharm中运用open函数打开txt文件后想要用pandas查看文本文件的前几列,如下

c24 = open('C://Users/二狗/Desktop/rna-seq report/feature_counts/count24_cut.txt')

然后就报错了

image

报错信息

Traceback (most recent call last):

解决方案:

经查询后发现head() 方法用于显示 pandas DataFrame 的前几行,但我尚未从文件创建 DataFrame,而要从文本文件创建 DataFrame,首先需要使用 pandas 的 read_csv() 方法将文件内容读入 pandas DataFrame 对象,代码如下:

c24 = pd.read_csv('C://Users/二狗/Desktop/rna-seq report/feature_counts/count24_cut.txt', sep='\t', index_col=0)

成功解决

image
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容