简体   繁体   中英

Pandas not exporting dataframe to csv

I have a script to output a whole bunch of CSVs to folder c:\\Scripts\\CSV. This particular script is looping through all of the dataframes and counting the usage of the top 100 words in the data set. The top 100 words and their count are added to a list, the dataframes are concatenated, and then the csv should export. The print contains the correct information, but the script doesn't output any file.

#! python3
import pandas as pd
import os

path = r'Scripts\\CSV\\'
directory = os.path.join("c:\\",path)
appended_data = []

for root,dirs,files in os.walk(directory):
     for file in files:
        if file.endswith(".csv"):
            thread = pd.read_csv(directory + file)
            thread.columns = ['num', 'id', 'body', 'title', 'url']
            s = pd.Series(''.join(thread['body']).lower().split()).value_counts()[:100]
            appended_data.append(s)
thatdata = pd.concat(appended_data)
#print(appended_data)
thatdata.to_csv = (directory + 'somename.csv')

Try using pathlib instead:

from pathlib import PureWindowsPath
directory = PureWindowsPath('c:/Scripts/CSV/')
for csv_f in directory.glob('**/*.csv'):
    # process inputs
target_path = directory / 'somename.csv'
thatdata.to_csv(target_path)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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