Home > front end >  Why Pandas imports .csv as dataframe and Excel as dict?
Why Pandas imports .csv as dataframe and Excel as dict?

Time:06-12

I'm trying to import a CSV and Excel (xlsx) to process, merge, compute... however, when using pandas to import each, I get different data types:

import pandas as pd

ach_data = pd.read_excel(r'C:\temp\SHC v0.34.xlsm', sheet_name=['ABA_Accounts'])
c2c_import = pd.read_csv(r'C:\temp\C2C Import File.csv')

print('Excel: '   str(type(ach_data)))
print('CSV: '   str(type(c2c_import)))

Output:

Excel: <class 'dict'>
CSV: <class 'pandas.core.frame.DataFrame'>

Any reason why?

CodePudding user response:

It's because you pass a list to sheet_name parameter, use sheet_name='ABA_Accounts' instead:

ach_data = pd.read_excel(r'C:\temp\SHC v0.34.xlsm', sheet_name='ABA_Accounts')

If you read the documentation about what is returned:

Returns DataFrame or dict of DataFrames

DataFrame from the passed Excel file. See notes in sheet_name argument for more information on when a dict of DataFrames is returned.

CodePudding user response:

Specifying a list as the sheet_name argument means that you want to read multiple sheets from the .xslm file as a dict of DataFrames.

An example from the docs:

[0, 1, "Sheet5"]: Load first, second and sheet named "Sheet5" as a dict of DataFrame

If you only want one dataframe, just pass it as a string: sheet_name='ABA_Accounts'.

  • Related