Almizar
Almizar

Reputation: 33

Append values into Pandas series

I want to extract a specific a line from a file. There are many files, resulting in many lines/values. I want to put all the values into a Pandas DataFrame.

For that, the aim is to put the lines called "11" into one column "11" (Series), the lines called "22" into the next column/Series "22" and the lines "33" into the last column/Series.

Aim:

         11     22     33
file1   0.4     4.1    6.2
file2   0.5     4.3    6.1
file3   0.6     4.6    6.1
file4   0.8     4.1    6.7
...     ...     ...    ...

Now, I just have:

import os
cwd=os.getcwd()
import pandas as pd
columns=["11","22","33"]
df=pd.DataFrame(columns=columns)
filenames=[]
for files in os.listdir(cwd):
    if files.startswith("file"):
        os.chdir(files)
        cwd2=os.getcwd()
        filenames.append(files)     
        for files2 in os.listdir(cwd2):
            if files2.startswith("Ausgab"):
                os.chdir(files2)
                o=open("yoyo.txt","r")
                i=0
                for line in o:
                    i=i+1
                    if i==1:
                        df["11"].append(line[15:40])
                    if i==2:
                        df["22"].append(line[15:40])
                    if i==3:
                        df["33"].append(line[15:40])

        os.chdir(cwd1)
df=pd.DataFrame(columns=columns,index=filenames)
df.to_csv("ttttest.csv")

The file listing does work, so the columns "11","22","33". But the part with df["11"].append doesnt work. How can I add values to a Pandas Series from a loop?

Upvotes: 2

Views: 489

Answers (1)

Stael
Stael

Reputation: 2689

I have a way that works. There was another discussion that I can't find right now about doing something similar, and this was one of the better answers.

instead of making a dataframe to begin with, you can make a dictionary of lists - in your case something like

d = {"11": [],"22": [],"33":[]}

when you come to append the data you do something like:

d["11"].append(line[15:40])

or perhaps

d["11"] += line[15:40]

(i'm not clear what your data will be in line - if it's a string I would have thought you want to split it and then add the whole list?)

finally you can make your dataframe, assuming all of your lists are the same length by calling df = pd.DataFrame(d)

Upvotes: 1

Related Questions