2

Every month I am downloading the whole ledger in csv format and saving the same to a folder. How I can read it in pandas as single dataframe

1

3 Answers 3

8

fthomson's answer but the last line should be

import pandas as pd
import glob
files = glob.glob("path/*.csv")

df = pd.DataFrame()
for f in files:
    csv = pd.read_csv(f)
    df = df.append(csv)
Sign up to request clarification or add additional context in comments.

1 Comment

This answer is NOT it. Never append DataFrames repeatedly in a loop as it repeatedly allocated memory! Every df.append copies the entire DataFrame. ni1o1's answer is to be used.
7

This is a faster way. Append small DataFrame after large DataFrame will cost a lot. So a better way is to append all DataFrame into one list and use pd.concat to concat all DataFrame.

import pandas as pd
import glob
files = glob.glob("path/*.csv")
df = []
for f in files:
    csv = pd.read_csv(f)
    df.append(csv)
df = pd.concat(df)

Comments

2

I'm doing this from memory but this is the general idea

import pandas as pd
import glob
files = glob.glob("path/*.csv")

df = pd.DataFrame()
for f in files:
    csv = pd.read_csv(f)
    df = df.append(csv)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.