James Bond
James Bond

Reputation: 7913

python pandas: how to return an empty dataframe in a loop?

to aggregate the content of a list of csv files , i normally do:

def getContent(fn):
    // do some thing with fn and generate a tempDataFrame
    return tempDataFrame


agg = pd.concat([getContent(x) for x in myListOfFiles])

i feel this is a very neat solution.

however, when something went wrong and i cannot find the fn or so, i need to return an empty tempDataFrame!

How can i return an empty dataframe in this case? can anyone share some experiences?

thanks!

Upvotes: 3

Views: 4781

Answers (1)

Elias Dorneles
Elias Dorneles

Reputation: 23806

As pd.concat ignores any None object, you can simply make your function getContent return None.

The only problem with this approach is if all items are None, it will throw an exception. If you want to prevent that, you can create an empty dataframe as the first argument with the desired columns:

>>> columns = ['col1', 'col2', 'col3']
>>> pd.concat([pd.DataFrame({k: [] for k in columns}), None, None])
    Empty DataFrame
Columns: [col1, col2, col3]
Index: []

[0 rows x 3 columns]

Upvotes: 5

Related Questions