【问题标题】:How can I write a function in python to write the file names of directory with the output of my function to a dataframe?如何在 python 中编写一个函数来将目录的文件名与我的函数输出写入数据帧?
【发布时间】:2021-07-13 20:15:38
【问题描述】:

我对 python 非常陌生,并试图了解如何使用遍历。

#我的有效代码-

import pandas as pd
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
df = pd.read_csv("1003285474_1003285465_0a54173ed4c58b7354e0dd48.csv",encoding="utf-8")
s = ' '.join(df['transcript'])

sid = SentimentIntensityAnalyzer()
sid.polarity_scores(s)
Out[68]: {'neg': 0.046, 'neu': 0.707, 'pos': 0.247, 'compound': 0.9922}

正如您在上面看到的,我有 2 个函数,一个连接列的所有行,另一个返回情感极性分数。我的目标是遍历一个文件夹并为该文件夹中的所有 csv 执行上述操作。 我的最终目标是拥有一个具有以下内容的数据框-

filename                                             neg    neu    pos     compound
1003285474_1003285465_0a54173ed4c58b7354e0dd48.csv   0.046  0.707  0.247   0.9922
1003285474_1003285465_0a54173ed4c58b7354e0dd41.csv   0.192  0.731  0.122   0.7222

我应该如何遍历所有 csv 文件并将上述结果应用于函数并将上述结果获取到所有这些 csv 的数据帧?

【问题讨论】:

    标签: python pandas glob traversal


    【解决方案1】:
    import os    
    from glob import glob
    import pandas as pd
    import nltk
    from nltk.sentiment.vader import SentimentIntensityAnalyzer
    
    # use glob to get a list of csv files in a folder
    files = glob('path/to/folder/*.csv')
    sid = SentimentIntensityAnalyzer()
    # use dict comprehension to apply you analysis
    data = {os.path.basename(file): sid.polarity_scores(' '.join(pd.read_csv(file, encoding="utf-8")['transcript'])) for file in files}
    # create a data frame from the dictionary above
    df = pd.DataFrame.from_dict(data, orient='index')
    

    【讨论】:

      【解决方案2】:

      首先,创建一个函数来包装您的分析:

      def analyse_data(file_path):
          df = pd.read_csv(file_path, encoding='utf-8')
          s = ' '.join(df['transcript'])
      
          sid = SentimentIntensityAnalyzer()
          score = sid.polarity_scores(s)
          score['filename'] = os.path.basename(file_path)
      

      此函数获取文件路径并在最终数据框中返回一行。一个示例返回是:

      {'filename': '1003285474_1003285465_0a54173ed4c58b7354e0dd48.csv', 'neg': 0.046, 'neu': 0.707, 'pos': 0.247, 'compound': 0.9922}
      

      然后,使用os.walk 遍历目录中的所有文件并应用该函数。

      def create_dataframe(root_dir):
          data = []
          for path, subdirs, files in os.walk(root_dir):
              for file_name in files:
                  full_path = os.path.join(path, file_name)
                  data.append(analyse_data(full_path))
      
          return pd.DataFrame(data)
      

      我认为root_dir及其子目录下只存在CSVs文件,因此在应用分析功能之前无需检查文件类型。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 2015-01-21
        • 1970-01-01
        • 2016-12-04
        • 1970-01-01
        相关资源
        最近更新 更多