I have a list that is created using two columns of a dataframe. I need to create a dictionary where keys will be the elements of the list and the values will be the elements of a column in the dataframe. Below is an Example that I just created. The dataframe I am using is large and so is the list.
data={'init':[1,2,1], 'term':[2,3,3], 'cost':[10,20,30]}
df=pd.DataFrame.from_dict(data)
link=[(1,2),(1,3),(2,3) ]
I need to create the following dictionary using the dataframe and list.
link_cost={(1,2): 10,(1,3):30,(2,3):20,}
Could anyone help me with this? Any comments or instruction would be appreciated.
CodePudding user response:
Let's try set_index
reindex
then Series.to_dict
:
d = df.set_index(['init', 'term'])['cost'].reindex(index=link).to_dict()
d
:
{(1, 2): 10, (1, 3): 30, (2, 3): 20}
set_index
with multiple columns will create a MultiIndex which can be indexed with tuples. Selecting a specific column and then reindexing
will allow the list link
to reorder/select specific values from the Series. Series.to_dict
will create the dictionary output.
Setup used:
import pandas as pd
df = pd.DataFrame({
'init': [1, 2, 1],
'term': [2, 3, 3],
'cost': [10, 20, 30]
})
link = [(1, 2), (1, 3), (2, 3)]
CodePudding user response:
Why are you even using pandas for this? You have the dict
right there:
link_cost = dict(zip(link, data['cost']}}
# or if you must use the dataframe it's the same
link_cost = dict(zip(link, df['cost']}}
{(1,2): 10, (2,3):20, (1,3):30}
CodePudding user response:
One approach is to use DataFrame.set_index
, Index.isin
and DataFrame.itertuples
:
import pandas as pd
data = {'init': [1, 2, 1], 'term': [2, 3, 3], 'cost': [10, 20, 30]}
df = pd.DataFrame.from_dict(data)
link = [(1, 2), (2, 3), (1, 3)]
cols = ["init", "term"]
new = df.set_index(cols)
res = dict(new[new.index.isin(link)].itertuples(name=None))
print(res)
Output
{(1, 2): 10, (2, 3): 20, (1, 3): 30}