Question

I haven't been able to find any information on this topic here, and would really appreciate your help! I'm pretty new to python, but here's what I have.

I have multiple file in a folder, and want to read them, transpose them, and then rewrite them into a new folder. I think I have everything going, but can't figure out how to rewrite everything.

here is my code:

path = 'C:\Users\Christopher\Documents\Clemson\Pleurodires\stability data\Es03\fixed\processed'
filenames = glob.glob(path + "/*.csv")
for filename in filenames:
    dfs = (pd.read_csv(filename))
    df = dfs.transpose()
    df.to_csv('transposed\' + 'Tr_' + filename)

the last line (i hope) should put all the new files in the folder called 'transposed', adding a Tr_ in front of the name which was loaded initially (ie if the file name was 'hello' it would now be 'Tr_hello' inside of the folder transposed).

What is happening when I run the code above, is that it says it works, but then the files don't exist anywhere in my computer. I've tried playing around with a variety of different ways to get the df.to_csv to work and this is the closest I've gotten

Edit

Thanks for everyone's help, I ended up combining a mix of Nanashi's and EdChun's code to get this, which works: (the final files are in the correct folder, and are called Tr_filename)

path = r'C:\Users\Christopher\Documents\Clemson\Pleurodires\stability data\Es03\fixed\processed'
filenames = glob.glob(path + "/*.csv")
for filename in filenames:
    short = os.path.split(filename)
    newfilename = 'Tr_%s' % short[-1]
    #print newfilename
    dfs = (pd.read_csv(filename))
    df = dfs.transpose()
    df.to_csv(os.path.join('transposed', newfilename))
Was it helpful?

Solution

A few things:

  1. filenames = glob.glob(path + "/*.csv") -- unless I'm wrong, that should be a backslash, not a forward-slash. Forward slashes are primarily used in Unix systems, etc. but definitely not in Windows where path names are concerned.

  2. Try printing out filename. It will give you the whole path as well. At the df.to_csv line, you're actually writing to path + filename + transposed + Tr + filename. You have to isolate the specific filename (using split or the os module may work).

I'm using Ubuntu, so this might not apply that accurately, but here's how I'll do it.

import pandas as pd
from glob import glob

path = "/home/nanashi/Documents/Python 2.7/Scrapers/Scrapy/itbooks"
filenames = glob(path + "/*.csv")

for filename in filenames:
    specname = filename.split("/")[-1]
    print filename
    print specname
    dfs = pd.read_csv(filename)
    df = dfs.transpose()
    df.to_csv("transposed/%s" % specname)

Result:

/home/nanashi/Documents/Python 2.7/Scrapers/Scrapy/itbooks/realestateau.csv
realestateau.csv
/home/nanashi/Documents/Python 2.7/Scrapers/Scrapy/itbooks/itbooks.csv
itbooks.csv
[Finished in 0.6s]

Screenshot of transposed file:

enter image description here

Let us know if this helps.

OTHER TIPS

Your code seems to have multiple errors try the following:

import os
path = r'C:\Users\Christopher\Documents\Clemson\Pleurodires\stability data\Es03\fixed\processed'
filenames = glob.glob(path + "/*.csv")
for filename in filenames:
    dfs = (pd.read_csv(filename))
    df = dfs.transpose()
    df.to_csv(os.path.join(r'transposed\Tr_', filename))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top