I'm new to Python. I have this kind of dictionary, from a geodesic output and i wonder if i can turn this into DataFrame or matrix? here's an example data, but what i'm working right now has more than 8000 data
{(0, 0): 0.0,
(0, 1): 1.3128088339744233,
(1, 0): 1.3128088339744233,
(1, 1): 0.0}
desired output would be a DataFrame that looks like this
or is there any other way to create distance matrix using geodesic from geopy.calculation?
Try this -
I have added additional entries to show how this approach scales to more rows and column indexes, and handles missing row, column indexes as well.
d = {(0, 0): 0.0,
(0, 1): 1.3128088339744233,
(1, 0): 1.3128088339744233,
(1, 1): 0.0}
df = pd.DataFrame(d.values(), index=d.keys()).unstack(-1).droplevel(0, axis=1)
print(df)
0 1
0 0.000000 1.312809
1 1.312809 0.000000
Additional test with missing rows and column indexes -
d = {(0, 0): 0.0,
(0, 1): 1.3128088339744233,
(1, 0): 1.3128088339744233,
(1, 1): 0.0,
(1, 2): 1.7, #More entries
(2, 1): 2.3} #More entries
df = pd.DataFrame(d.values(), index=d.keys()).unstack(-1).droplevel(0, axis=1)
print(df)
0 1 2
0 0.000000 1.312809 NaN
1 1.312809 0.000000 1.7
2 NaN 2.300000 NaN
df = pd.DataFrame.from_dict(d, orient='index')
df.index = pd.MultiIndex.from_tuples(df.index)
df = df.unstack(-1).droplevel(0, axis=1)
Firstly, install pandas
import pandas as pd
Make use of pd.DataFrame.from_dict()
function. It has two arguments you need. Columns which specify your column name and orient for row. So go through dictionary key and assign first element as column(row) names and second as row(column).
Source is here
Use this:
dicti = {(0, 0): 0.0,
(0, 1): 1.3128088339744233,
(1, 0): 1.3128088339744233,
(1, 1): 0.0}
rows = [a[0] for a in list(dicti.keys())]
columns = [a[1] for a in list(dicti.keys())]
#only storing unique values for rows and columns
rows = list(dict.fromkeys(rows))
columns = list(dict.fromkeys(columns))
#Initializing empty dict in order to create dataframe from it
df_dict = {}
for val in columns:
df_dict[val] = []
for k,v in dicti.items():
df_dict[k[1]].append(v)
df = pd.DataFrame(df_dict, columns=columns, index=rows) #creating DataFrame
df
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.