简体   繁体   English

Python 和 Pandas:将数据写入 csv 中的特定列

[英]Python & Pandas: Writing data to specific columns in csv

While using Python and Pandas, I'm running a script that analyzes txt files for word count and lexile scores.在使用 Python 和 Pandas 时,我正在运行一个脚本来分析 txt 文件的字数和词法分数。 I can successfully run the script and write to csv.我可以成功运行脚本并写入 csv。 However, my output delivers unexpected values, and I'm having difficulty writing the data to the specific column.但是,我的输出提供了意外的值,并且我无法将数据写入特定列。

Here is code:这是代码:

import pandas as pd
import textstat
import csv

header = ["word_count", "flech"]

with open('data.csv', 'w', encoding='UTF8') as f:
    writer = csv.writer(f)

    writer.writerow(header)
    
for text_number in range(0, 2):

    f = open(f'\TXTs\text_{text_number}.txt', 'r')

    if f.mode == 'r':
        contents = f.read()
        
    text_data = (contents)

    word_count = textstat.lexicon_count(text_data, removepunct=True)
    flech = textstat.flesch_kincaid_grade(text_data)
   
    wc = pd.DataFrame([word_count])
    fl = pd.DataFrame([flech])
    
    def wc_count():
        wc.to_csv('output.csv', mode="a", header="word_count", index=False)
        
    def fl_count():
        fl.to_csv('output.csv', mode="a", header="flech", index=False)

    wc_count()
    fl_count()

I'd like the output to look like this, with the 2 & 271 values in the "word_count" column, and the -3.1 and 13 in the "flech" column:我希望输出看起来像这样,“word_count”列中有2271值,“flech”列中有-3.113

word_count, flech
2, -3.1
271, 13

However, the output produced looks like this:但是,产生的输出如下所示:

word_count, flech
    
0   
2   
0   
-3.1    
0   
271 
0   
13  

Clearly, I've got some problems with my output.显然,我的输出有一些问题。 Any assistance would be greatly appreciated.任何帮助将不胜感激。

Instead of creating two dataframe try creating one and write in csv.与其创建两个数据框,不如尝试创建一个并写入 csv。

flech = textstat.flesch_kincaid_grade(text_data) # change after this line
output_df = pd.DataFrame({"word_count":[word_count], "flech":[flech])
output_df.to_csv('output.csv', mode="a", index=False)

It looks like you're going through great lengths for something that seems quite straightforward.看起来您正在为看似很简单的事情费尽心思。 Just use pandas' I/O function to read/write your data: pandas.read_csv and pandas.DataFrame.to_csv只需使用 pandas 的 I/O 函数来读/写您的数据: pandas.read_csvpandas.DataFrame.to_csv

It is hard to give you the exact code without the data, but try something like:如果没有数据,很难为您提供确切的代码,但请尝试以下操作:

with open(f'\TXTs\text_{text_number}.txt', 'r') as f:
    text_data = f.read()

word_count = textstat.lexicon_count(text_data, removepunct=True)
flech = textstat.flesch_kincaid_grade(text_data)

df = pd.DataFrame({'word_count': word_count, 'flech': flech})

df.to_csv('output.csv', index=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM