user12314098
user12314098

Reputation:

Appending rows to empty Pandas Dataframe

I read on this site this.pointer.com that you should append a dictionary to an empty dataframe like so

df = pd.DataFrame(columns=['count', 'name'])

f = open("fbnames.txt", "r")
count = 0
save_every = 1

for line in f:
    count += 1
    split = line.split()
    df.append({'count':split[0], 'name':split[1].capitalize()}, ignore_index=True)
    print(df)

df.to_csv("fbnames.csv")

But the 'print(df)' just prints an empty dataframe everytime. What's going on? I'm just trying to read a txt file line by line then turn the line into a pandas dataframe row and append it.

Upvotes: 1

Views: 833

Answers (1)

jezrael
jezrael

Reputation: 862661

Because DataFrame.append not working inplace, is necessary assign output back:

df = df.append({'count':split[0], 'name':split[1].capitalize()}, ignore_index=True)

Upvotes: 2

Related Questions