Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transfer pandas dataframe column names to dictionary

I'm trying to convert a pandas dataframe column names into a dictionary. Not so worried about the actual data in the dataframe.

Say I have an example dataframe like this and I'm not too worried about index just now:

Col1 Col2 Col3 Col4
--------------------
 a    b    c    a
 b    d    e    c

I'd like to get an output of a dictionary like:

{'Col1': 0, 'Col2': 1, 'Col3': 2, 'Col4': 3}

Not too worried about the order they get printed out, as long as the assigned keys in the dictionary keep the order for each column name's order.

like image 474
IJ.K Avatar asked Feb 13 '19 16:02

IJ.K


People also ask

How do I convert a pandas DataFrame to a dictionary?

Use DataFrame. To convert pandas DataFrame to Dictionary object, use to_dict() method, this takes orient as dict by default which returns the DataFrame in format {column -> {index -> value}} . When no orient is specified, to_dict() returns in this format.

Can we convert DataFrame to dictionary in Python?

to_dict() method is used to convert a dataframe into a dictionary of series or list like data type depending on orient parameter. Parameters: orient: String value, ('dict', 'list', 'series', 'split', 'records', 'index') Defines which dtype to convert Columns(series into).

How do I convert two columns to dictionary in pandas?

Another approach to convert two column values into a dictionary is to first set the column values we need as keys to be index for the dataframe and then use Pandas' to_dict() function to convert it a dictionary. This creates a dictionary for all columns in the dataframe.


2 Answers

That is straight forward with a comprehension as:

Code:

{c: i for i, c in enumerate(df.columns)}

Test Code:

import pandas as pd

df = pd.DataFrame({'date': ['2015-01-01', '2015-01-02', '2015-01-03'],
                   'value': ['a', 'b', 'c'],
                   'num': [1, 2, 3]
                   })

print(df)
print({c: i for i, c in enumerate(df.columns)})

Results:

         date  num value
0  2015-01-01    1     a
1  2015-01-02    2     b
2  2015-01-03    3     c

{'date': 0, 'num': 1, 'value': 2}
like image 86
Stephen Rauch Avatar answered Sep 22 '22 13:09

Stephen Rauch


Instead of using enumerate as @StephenRauch has posted, you could also use a pandas.Index method, get_loc:

{i:df.columns.get_loc(i) for i in df.columns}

Using Stephen's setup:

import pandas as pd

df = pd.DataFrame({'date': ['2015-01-01', '2015-01-02', '2015-01-03'],
                   'value': ['a', 'b', 'c'],
                   'num': [1, 2, 3]
                   })

print(df)
print({i:df.columns.get_loc(i) for i in df.columns})

Output:

         date value  num
0  2015-01-01     a    1
1  2015-01-02     b    2
2  2015-01-03     c    3

{'date': 0, 'value': 1, 'num': 2}
like image 21
Scott Boston Avatar answered Sep 25 '22 13:09

Scott Boston