Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert dataframe row to dict

Tags:

I have datarame like the sample data below. I'm trying to convert one row from the dataframe in to a dict like the desired output below. But when I use to_dict I get the indice along with the column value. Does anyone know how to get convert the row to a dict like the desired output? Any tips greatly appreciated.

Sample data:

print(catStr_df[['Bottle Volume (ml)', 'Pack']][:5])

      Bottle Volume (ml)  Pack
595                  750    12
1889                 750    12
3616                1000    12
4422                 750    12
5022                 750    12


Code:

v = catStr_df[catStr_df['Item Number']==34881][['Bottle Volume (ml)', 'Pack']]\
.drop_duplicates(keep='first').to_dict()

v

Output:

{'Bottle Volume (ml)': {9534: 1000}, 'Pack': {9534: 12}}

Desired output:

{'Bottle Volume (ml)': 1000, 'Pack': 12}
like image 804
user3476463 Avatar asked May 29 '18 03:05

user3476463


People also ask

How do I convert a row into a DataFrame dictionary?

DataFrame to dict by row index When we have a DataFrame with row indexes and if we need to convert the data of each row from DataFrame to dict , we can use the index parameter of the DataFrame. to_dict() function. It returns a list of dictionary objects. A dict is created for each row.

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 a data table to a dictionary in Python?

First, search for the table header and split on spaces to define a list. Second, search for the virtual drive with "number/number" and split on spaces to define the second list. However, 'Size' will need to be special as it will need to ignore the space between number and "TB".

How to convert each row of a Dataframe to Dict in Python?

When we have a DataFrame with row indexes and if we need to convert the data of each row from DataFrame to dict, we can use the index parameter of the DataFrame.to_dict () function. It returns a list of dictionary objects. A dict is created for each row. Where the key is a row index, and the value is dict of column label and data.

How do you convert a Dataframe to a dictionary?

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=)

How to convert a Dataframe to a list of values?

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.

How do I convert pandas to a dictionary?

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).


1 Answers

Try adding .to_dict('records')[0] to the row you want

catStr_df[catStr_df['Item Number']==34881].to_dict('records')[0]
like image 78
nimrodz Avatar answered Sep 28 '22 03:09

nimrodz