As part of my demonstration of the effect of packet loss rates on TCP performance, I build a comma separated variable (CSV) file which has data rate as a function of several variables: 1 packet loss rate % 1 Network delay 1 File size 1protocol (HTTP, HTTPS, FTP)
In [6]:
import pandas as pd
In [2]:
d=dict()
d={ 'gas': { 'a': { 7: {'IPv4': 20.7, 'IPv6': -20.7},
8: {'IPv4': 20.8, 'IPv6': -20.8},
9: {'IPv4': 20.9, 'IPv6': -20.9}},
'b': { 7: {'IPv4': 21.7, 'IPv6': -21.7},
8: {'IPv4': 21.8, 'IPv6': -21.8},
9: {'IPv4': 21.9, 'IPv6': -21.9}},
'c': { 7: {'IPv4': 22.7, 'IPv6': -22.7},
8: {'IPv4': 22.8, 'IPv6': -22.8},
9: {'IPv4': 22.9, 'IPv6': -22.9}}},
'liquid': { 'a': { 7: {'IPv4': 10.7, 'IPv6': -10.7},
8: {'IPv4': 10.8, 'IPv6': -10.8},
9: {'IPv4': 10.9, 'IPv6': -10.9}},
'b': { 7: {'IPv4': 11.7, 'IPv6': -11.7},
8: {'IPv4': 11.8, 'IPv6': -11.8},
9: {'IPv4': 11.8, 'IPv6': -11.8}},
'c': { 7: {'IPv4': 12.7, 'IPv6': -12.7},
8: {'IPv4': 12.8, 'IPv6': -12.8},
9: {'IPv4': 12.9, 'IPv6': -12.9}}},
'solid': { 'a': { 7: {'IPv4': 0.7, 'IPv6': -0.7},
8: {'IPv4': 0.8, 'IPv6': -0.8},
9: {'IPv4': 0.9, 'IPv6': -0.9}},
'b': { 7: {'IPv4': 1.7, 'IPv6': -1.7},
8: {'IPv4': 1.8, 'IPv6': -1.8},
9: {'IPv4': 1.8, 'IPv6': -1.8}},
'c': { 7: {'IPv4': 2.7, 'IPv6': -2.7},
8: {'IPv4': 2.8, 'IPv6': -2.8},
9: {'IPv4': 2.9, 'IPv6': -2.9}}}}
In [3]:
import pprint
pp=pprint.PrettyPrinter(indent=2, width=80)
In [4]:
pp.pprint(d)
In [23]:
df=pd.DataFrame.from_dict(d, orient="index").rename_axis('phase')
print(df)
print(df.names)
In [30]:
df=pd.DataFrame.from_dict(d, orient="index").rename_axis('phase')
print(df)
print(40*'=')
df.rename_axis('alpha', axis=1, inplace=True)
print(df)
In [34]:
# This is adapted from https://stackoverflow.com/questions/47416113/how-to-build-a-multiindex-pandas-dataframe-from-a-nested-dictionary-with-lists
d2 = {(i,j,k,l): d[i][j][k][l]
for i in d.keys()
for j in d[i].keys()
for k in d[i][j].keys()
for l in d[i][j][k].keys()
}
In [35]:
d2
Out[35]:
In [39]:
mux = pd.MultiIndex.from_tuples(d2.keys())
df = pd.DataFrame(list(d2.values()), index=mux)
print(mux)
In [38]:
df
Out[38]:
In [46]:
# This is adapted from https://stackoverflow.com/questions/47416113/how-to-build-a-multiindex-pandas-dataframe-from-a-nested-dictionary-with-lists
d3 = {(i,j,k): d[i][j][k]
for i in d.keys()
for j in d[i].keys()
for k in d[i][j].keys()
}
mux = pd.MultiIndex.from_tuples(d3.keys())
mux
df = pd.DataFrame(list(d3.values()), index=mux)
df
Out[46]:
In [60]:
# Adapted from https://stackoverflow.com/questions/50665996/pandas-read-in-multiindex-data-from-csv-file
# See also https://stackoverflow.com/questions/24519304/reading-csv-files-w-multiindex for another way to do it.
dfcsv = pd.read_csv("high_dimension_data.csv", index_col=[0,1,2,3])
dfcsv
Out[60]:
In [2]:
lk=["solid","b",17,"IPv2"]
lk
tk=tuple(lk)
tk
Out[2]: