Suppose I have a Dataframe df :
Label1 Label2 Label3
key1 col1value1 col2value1
key2 col1value2 col2value2
key3 col1value3 col2value3
dict1 = df.set_index('Label1').to_dict()
This works when we have 2 columns..
Expected Output:
my_dict = {key1: [col1value1,col2value1] , key2: [ col1value2,col2value2] , key3:[col1value3,col2value3] }
Can I use to_dict
on Dataframe df to have a key with 2 other columns as values in form of list ??
Pandas is one of those packages and makes importing and analyzing data much easier. Pandas .to_dict() method is used to convert a dataframe into a dictionary of series or list like data type depending on orient parameter. Syntax: DataFrame.to_dict(orient=’dict’, into=)
It is a case when we have DataFrame, which needs to be converted into the dictionary object such that column label should be the keys in the dictionary, and all the columns’ data should be added into the resultant dict as a list of values against each key. In that case, we can use 'list' parameter of the DataFrame.to_dict () function.
After analyzing the data, we need to convert the resultant DataFrame back to its original format like CSV files or a dictionary. Or sometimes, we need to convert it into some other form. Pandas have a DataFrame.to_dict () function to create a Python dict object from DataFrame. into: It is used to define the type of resultant dict.
Pandas .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).
Well you could use a dictionary comprehension and iterrows:
print {key:row.tolist() for key,row in df.set_index('Label1').iterrows()}
{'key3': ['col1value3', 'col2value3'],
'key2': ['col1value2', 'col2value2'],
'key1': ['col1value1', 'col2value1']}
Also, I think the following will work too:
df = df.set_index('Label1')
print df.T.to_dict(outtype='list')
{'key3': ['col1value3', 'col2value3'],
'key2': ['col1value2', 'col2value2'],
'key1': ['col1value1', 'col2value1']}
Update as of fall 2017; outtype
is no longer the keyword argument. Use orient instead:
In [11]: df.T.to_dict(orient='list')
Out[11]:
{'key1': ['col1value1', 'col2value1'],
'key2': ['col1value2', 'col2value2'],
'key3': ['col1value3', 'col2value3']}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With