将每行 Pandas 数据帧写入一个新的文本文件 - pythonic 方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33620132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:11:50  来源:igfitidea点击:

Write each row of pandas dataframe into a new text file - pythonic way

pythonpandasrows

提问by Wolf

I was trying to google up if there's a way to parse a pandas dataframe row wise and write the contents of each row into a new text file. My dataframe consists of a single column called Reviews.

如果有一种方法可以逐行解析 Pandas 数据帧并将每一行的内容写入新的文本文件,我正试图通过谷歌搜索。我的数据框由一个名为 Reviews 的列组成。

Review classification

评论分类

I'm looking to do some sentiment analysis on movie reviews and that I need each review to be in a separate text file. Can somebody help me here.

我希望对电影评论进行一些情感分析,并且我需要将每个评论都放在一个单独的文本文件中。有人可以在这里帮助我。

回答by Wolf

I've written something like this and it works. anyways thanks for your inputs guys

我写了这样的东西,它的工作原理。无论如何感谢您的投入

for index, row in p.iterrows():
    if i > len(p):
       break
    else:
       f = open(str(i)+'.txt', 'w')
       f.write(row[0])
       f.close()
       i+=1

where p is a dataframe.

其中 p 是一个数据帧。

回答by Leb

It's still inefficient, but since it's required here's one possible solution.

它仍然效率低下,但由于它是必需的,因此这是一种可能的解决方案。

import pandas as pd
from io import StringIO

data="""
column1 column2
c1 c2
c3 c4
c5 c6
"""

df = pd.read_csv(StringIO(data), delimiter='\s+')

i=0
for row in df.values:
    filename = 'testdir/review{}.csv'.format(i)
    row.tofile(filename, sep=",", format="%s")
    i+=1

This will take the values as an array and writethe data to a csv file named review0.csv, review1.csv... Another solution is to use pd.to_csvwithin the loop and specify the chunk

这会将值作为数组并将数据写入名为 的 csv 文件review0.csvreview1.csv...另一种解决方案是pd.to_csv在循环中使用并指定chunk