Using python comprehension with dictionary to assign values to pandas dataframe

commentallez-vous

Say I am trying to build a dataframe to print out like a table for checking sectors:

SectorDescription   SectorCode
0   State Energy Data Systems   SEDS
1   Coal Data   COAL
2   Petroleum Data  PET
3   Natural Gas Data    NG
4   Electricity Data    ELEC
5   Petroleum Imports Data  PET_IMPORTS
6   Short-Term Energy Outlook Data  STEO
7   International Energy Data   INTL
8   Annual Energy Outlook Data  AEO

Right now I have:

QuandlEIASector = {"State Energy Data Systems":"SEDS",
                  "Coal Data":"COAL",
                  "Petroleum Data":"PET",
                  "Natural Gas Data":"NG",
                  "Electricity Data":"ELEC",
                  "Petroleum Imports Data":"PET_IMPORTS",
                  "Short-Term Energy Outlook Data":"STEO",
                  "International Energy Data":"INTL",
                  "Annual Energy Outlook Data":"AEO"}

What I did is to:

QuandlEIASectorList = pd.DataFrame()
QuandlEIASectorList['SectorDescription'] = QuandlEIASector.keys()
QuandlEIASectorList['SectorCode'] = QuandlEIASector.values()
QuandlEIASectorList

But is there anyway quicker with python's comprehension one-liner to assign column values to a pandas dataframe?

jezrael

Create Series and then convert to DataFrame:

QuandlEIASectorList = (pd.Series(QuandlEIASector)
                         .rename_axis('SectorDescription')
                         .reset_index(name='SectorCode'))

Similar:

QuandlEIASectorList = (pd.Series(QuandlEIASector, name='SectorCode')
                         .rename_axis('SectorDescription')
                         .reset_index())

Your code should be used with DataFrame constructor:

QuandlEIASectorList = pd.DataFrame({'SectorDescription':list(QuandlEIASector.keys()),
                                    'SectorCode': list(QuandlEIASector.values())})

Or:

QuandlEIASectorList = pd.DataFrame(list(QuandlEIASector.items()), 
                                   columns=['SectorDescription','SectorCode'])

Performance for 10k keys:

QuandlEIASector = dict(zip([f'{x} data' for x in np.arange(10000)], 
                           [f'{x} keys' for x in np.arange(10000)]))

In [73]: %%timeit
    ...: QuandlEIASectorList = pd.DataFrame()
    ...: QuandlEIASectorList['SectorDescription'] = QuandlEIASector.keys()
    ...: QuandlEIASectorList['SectorCode'] = QuandlEIASector.values()
    ...: 
5.94 ms ± 52.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [74]: %%timeit
    ...: (pd.Series(QuandlEIASector)
    ...:    .rename_axis('SectorDescription')
    ...:    .reset_index(name='SectorCode'))
    ...:                          
5.37 ms ± 261 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [75]: %%timeit
    ...: (pd.Series(QuandlEIASector, name='SectorCode')
    ...:    .rename_axis('SectorDescription')
    ...:    .reset_index())
    ...:                          
5.34 ms ± 211 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [76]: %%timeit
    ...: pd.DataFrame({'SectorDescription':list(QuandlEIASector.keys()),
    ...:               'SectorCode': list(QuandlEIASector.values())})
    ...:                                    
2.26 ms ± 20.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [77]: %%timeit
    ...: pd.DataFrame(list(QuandlEIASector.items()), 
    ...:              columns=['SectorDescription','SectorCode'])
    ...:                                    
3.15 ms ± 38.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

fill pandas values dictionary using list comprehension

Python consolidate dictionary keys and values using comprehension

Altering 10% of Values in Dictionary Comprehension using Python

Assign selected values to Pandas Series using dictionary

Assign values to existing dictionary keys as the dictionary is being made using dictionary comprehension

remapping multiple column values with multiple dictionary in dataframe using python pandas

Python Pandas Assign a Dictionary Value to a Dataframe Column Based on Dictionary Key

Convert dictionary comprehension to python dataframe

python, selectively pop values from one dictionary into another using comprehension

python using for loop to update nested dictionary values with comprehension

Filtering pandas dataframe using dictionary for column values

Set specific rows values to column in pandas dataframe using list comprehension

Python dictionary to pandas dataframe missing key values

Dictionary Comprehension within pandas dataframe column

How to assign multiple values to a key in a dictionary, multiple times using pandas

Assign values of a list to cell of dataframe using .loc in a pandas dataframe

Is there a dictionary comprehension that can retrieve key-values for each row of a pandas dataframe?

Converting Python dictionary using dictionary comprehension

How to style a Python Pandas Dataframe using dictionary

Recursive convert values to string using dictionary comprehension

creating dictionary containing list values using a comprehension

Building a dictionary with lists as values using list comprehension

Python Pandas Dataframe to Dictionary (Column Values to Keys/Values)

Pandas data frame to dictionary using dict comprehension

assign list values to dictionary python

Python DataFrame: Replace values using dictionary, convert NaN if not in dictionary

Python comprehension for dictionary with String key and List values

Nested Dictionary Comprehension in Python with List of Values

Assign brackets to values in pandas dataframe