Home > Back-end >  Extract element from a dictionary and create a dataframe
Extract element from a dictionary and create a dataframe

Time:12-31

I have a dictionary like this:

{'BTCTRY': {'price': '634834.00000000', 'time': datetime.datetime(2021, 12, 31, 10, 12, 53, 480197)}, 'BNBTRY': {'price': '6898.00000000', 'time': datetime.datetime(2021, 12, 31, 10, 12, 53, 480197)}, 'BUSDTRY': {'price': '13.08100000', 'time': datetime.datetime(2021, 12, 31, 10, 12, 53, 480197)}

and I would like to create from this dictionary a dataframe containing in the first column the pairname and in the second one the price element, like this:

Column0   Column1
BTCTRY    634834.0000000
BNBTRY    6898.0000000

What is the most correct way of doing this?

CodePudding user response:

You can try this:

import datetime

d = {'BTCTRY': {'price': '634834.00000000', 
            'time': datetime.datetime(2021, 12, 31, 10, 12, 53, 480197)
           }, 
 'BNBTRY': {'price': '6898.00000000', 
            'time': datetime.datetime(2021, 12, 31, 10, 12, 53, 480197)
           }, 
 'BUSDTRY': {'price': '13.08100000', 
             'time': datetime.datetime(2021, 12, 31, 10, 12, 53, 480197)
            }
}

df = pd.DataFrame.from_dict(d).T
df
# Output
#         price             time
#BTCTRY   634834.00000000   2021-12-31 10:12:53.480197
#BNBTRY   6898.00000000     2021-12-31 10:12:53.480197
#BUSDTRY  13.08100000       2021-12-31 10:12:53.480197

df.drop(["time"], axis=1)
# Output
#           price
#BTCTRY     634834.00000000
#BNBTRY     6898.00000000
#BUSDTRY    13.08100000

CodePudding user response:

I recommend using the pandas library and the pandas.DataFrame.from_dict method (see https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.from_dict.html)
You should specify the argument 'orient' to index.

data = {'BTCTRY': {'price': '634834.00000000', 'time': datetime.datetime(2021, 12, 31, 10, 12, 53, 480197)}, 'BNBTRY': {'price': '6898.00000000', 'time': datetime.datetime(2021, 12, 31, 10, 12, 53, 480197)}, 'BUSDTRY': {'price': '13.08100000', 'time': datetime.datetime(2021, 12, 31, 10, 12, 53, 480197)}}

df = pd.DataFrame.from_dict(data, orient='index')
print(df)

Best !

CodePudding user response:

df = pd.DataFrame([(k,v['price']) for k,v in dic.items()], columns = ['Column0','Column1'])

CodePudding user response:

I would try this:

import datetime
import pandas as pd

dictis = {
    'BTCTRY': {
        'price': '634834.00000000', 'time': datetime.datetime(2021, 12, 31, 10, 12, 53, 480197)
    }
    , 'BNBTRY': {
        'price': '6898.00000000', 'time': datetime.datetime(2021, 12, 31, 10, 12, 53, 480197)
    }
    , 'BUSDTRY': {
        'price': '13.08100000', 'time': datetime.datetime(2021, 12, 31, 10, 12, 53, 480197)
    }
}

values = [
    [key, next(iter(value.values()))]
    for key, value in dictis.items()
]

columns = ["Column 1", "Column 2"]

df = pd.DataFrame(
    data=values
    , columns=columns
)

print(df)

Output:

  Column 1         Column 2
0   BTCTRY  634834.00000000
1   BNBTRY    6898.00000000
2  BUSDTRY      13.08100000
  • Related