Home > Net >  pandas dataframe doesn't add all elements
pandas dataframe doesn't add all elements

Time:09-16

I'm trying this code on a folder with bunch of 'txt' files

import pandas as pd
import os

for review in os.listdir('ebert_reviews'):
    with open(os.path.join('ebert_reviews',review),encoding='utf-8') as file:
        list_dir=[]
        title = file.readline()[:-1]
        url = file.readline()[:-1]
        review_text = file.read()
        list_dir.append({'title':title,
                         'url':url,
                         'review_text':review_text})
        #print(list_dir)
df = pd.DataFrame(list_dir)
print(df)

the dataframe only saves one of the rows and it's not even the last one, how can I add every entry to it?!

CodePudding user response:

This should work

import pandas as pd
import os

list_dir=[]
for review in os.listdir('ebert_reviews'):
    with open(os.path.join('ebert_reviews',review),encoding='utf-8') as file:
        title = file.readline()[:-1]
        url = file.readline()[:-1]
        review_text = file.read()
        list_dir.append({'title':title,
                         'url':url,
                         'review_text':review_text})
        #print(list_dir)
df = pd.DataFrame(list_dir)
print(df)
  • Related