首页 > 解决方案 > 数据框转字符串

问题描述

import sys
if sys.version_info[0] < 3: 
    from StringIO import StringIO
else:
    from io import StringIO
import pandas as pd
TESTDATA = StringIO(txt)
df = pd.read_csv(TESTDATA,names=['col1'])

在哪里

txt="The lion (Panthera leo) is a species in the family Felidae;it is a muscular, deep-chested cat with a short, rounded head, a reduced neck and round ears, and a hairy tuft at the end of its tail. The lion is sexually dimorphic; males are larger than females with a typical weight range of 150 to 250 kg (330 to 550 lb) for males and 120 to 182 kg (265 to 400 lb) for females. "

当我运行上面的代码时,我得到的输出为:

The lion (Panthera leo) is a species in the family Felidae;it is a muscular deep-chested cat with a short   rounded head    a reduced neck and round ears   and a hairy tuft at the end of its tail

我得到 4 个不同的列,最后一列标记为 col1。但我想要的是包含完整数据的单列。如何实现?我想将 txt 数据转换为具有单列的数据框。

标签: pythonpandasdataframestringio

解决方案


当您使用pd.read_csv默认分隔符读取数据时,如果您想通过不同的分隔符将其拆分或使用不在文件中的分隔符来忽略所有分隔符,则,需要显式传递sep=';'pd.read_csv(TESTDATA, sep=';')sep='###'


推荐阅读