Yung Bas
Yung Bas

Reputation: 31

Pandas DataFrame to Dictionary with Tuples as Key and Values

I need help doing the following:

I have a CSV file as the following, loaded into a dataframe 'df'. There are multiple regions, different values for Memory, vCPUs and Storage corresponding to each 'Name'. There are 1700 rows in this dataframe.

The dataframe with the CSV values loaded in

I need to create a dictionary that has the following:

Key is a tuple with two elements: Name, and Region

Value of the dictionary is a tuple: Windows On-demand cost and Linux On demand cost

Ultimately, I want to create a program which does the following: The user inputs a certain CPU and Ram and Storage, and the program will sort through the data and pull the Name, as well as Windows and Linux prices for that processor if there is a match, or if not, will pull the processor closest to the inputted values. Thanks!

Name    Region  API Memory  vCPUs   Storage Linux   Windows
0   M1 General Purpose Small    US West - NorCal    m1.small    1.7 GiB 1 vCPUs 160 GiB $0.047000 hourly    $0.078000 hourly
1   M1 General Purpose Medium   US West - NorCal    m1.medium   3.75 GiB    1 vCPUs 410 GiB $0.095000 hourly    $0.157000 hourly
2   M1 General Purpose Large    US West - NorCal    m1.large    7.5 GiB 2 vCPUs 840 GiB $0.190000 hourly    $0.314000 hourly
3   M1 General Purpose Extra Large  US West - NorCal    m1.xlarge   15.0 GiB    4 vCPUs 1680 GiB    $0.379000 hourly    $0.627000 hourly
4   C1 High-CPU Medium  US West - NorCal    c1.medium   1.7 GiB 2 vCPUs 350 GiB $0.148000 hourly    $0.228000 hourly

Upvotes: 1

Views: 1509

Answers (2)

rer
rer

Reputation: 1268

I would try something like this:

outdict = {k: (gdf['Windows On Demand cost'].item(), 
               gdf['Linux On Demand cost'].item())
           for k, gdf in df.groupby(['Name', 'Region'])}

Upvotes: 0

ababuji
ababuji

Reputation: 1731

Here is the part that creates the dictionary

tempDict = {}

for i in df.index:

    key = (df.at[i, 'Name'] ,df.at[i, 'Region']) #Rename columns accordingly
    value = (df.at[i, 'Windows On-demand cost'] ,df.at[i, 'Linux On demand cost']) #Rename columns accordingly

    dictionary = {key: value}
    tempDict.update(dictionary)

print(tempDict)

Upvotes: 1

Related Questions