我有一堆txt
假新闻文本文件,我想把它们放在熊猫数据框中。但是,在单个文本文件中,第一行和第三行是分开的,这对于所有csv文件都是一样的。但是,我希望他们在熊猫数据帧中进行简单的数据操作。我试着跟踪我的数据:
cols, vals = zip(*[line.split('\n') for line in StringIO("biz01.fake.txt").read().split('\n\n')])
s = pd.Series(vals, cols)
s.index = [s.groupby(level=0).cumcount(), s.index]
s.unstack()
但我犯了一个错误,我不明白为什么。你知道吗
ValueError: not enough values to unpack (expected 2, got 1)
有人能帮我怎么做到吗?有什么解决办法吗?你知道吗
我的原始数据如下:
下面是txt文件列表(在我的本地驱动器上)的样子: list of txt files
所需输出示例
1 first_row second_row
2 headers_1 some text
3 headers_2 some texts
我在flies上放了一些示例文本文件。有人能帮我怎么做吗?你知道吗?你知道吗
新更新:
当我尝试以下解决方案时,得到了这种类型的输出:
在我的输入txt文件中,第二行是一个很长的文本,我只需要两列的dataframe和默认的列名。有更好的主意吗?你知道吗
可以使用
pandas
读入文件,然后删除空行。你知道吗例如,这里有一个文本文件新闻.txt““
然后我用密码
哪些输出
相关问题 更多 >
编程相关推荐