Home > Back-end >  how can I turn cell into a dataframe
how can I turn cell into a dataframe

Time:11-18

I have this data:

df['profile'] = {
    'symbol': 'AAPL', 
    'price': 150.72, 
    'beta': 1.246644, 
    'volAvg': 89576498, 
    'mktCap': 2397668846469, 
    'lastDiv': 0.91, 
    'range': '129.04-182.94', 
    'changes': 1.93, 
    'companyName': 'Apple Inc.', 
    'currency': 'USD', 
    'cik': '0000320193', 
    'isin': 'US0378331005', 
    'cusip': '037833100',
    'isFund': False}

how do i break this into a dataframe with headers of symbol, price, beta, etc and have the one row with the values?

CodePudding user response:

I guess you have something like this:

df = pd.DataFrame({
    'ID' : 0, 
    'status' : [{
        'symbol': 'AAPL', 
        'price': 150.72, 
        'beta': 1.246644, 
        'volAvg': 89576498, 
        'mktCap': 2397668846469, 
        'lastDiv': 0.91, 
        'range': '129.04-182.94', 
        'changes': 1.93, 
        'companyName': 'Apple Inc.', 
        'currency': 'USD', 
        'cik': '0000320193', 
        'isin': 'US0378331005', 
        'cusip': '037833100',
        'isFund': False}]
})
print(df)

   ID                                             status
0   0  {'symbol': 'AAPL', 'price': 150.72, 'beta': 1....

Convert the status column to a new dataframe like this:

out = df['status'].apply(pd.Series)
print(out)
  symbol   price      beta    volAvg         mktCap  lastDiv          range  changes companyName currency         cik          isin      cusip  isFund
0   AAPL  150.72  1.246644  89576498  2397668846469     0.91  129.04-182.94     1.93  Apple Inc.      USD  0000320193  US0378331005  037833100   False

CodePudding user response:

example

data = {'symbol': 'AAPL',
        'price': 150.72,
        'beta': 1.246644,
        'volAvg': 89576498,
        'mktCap': 2397668846469,
        'lastDiv': 0.91,
        'range': '129.04-182.94',
        'changes': 1.93,
        'companyName': 'Apple Inc.',
        'currency': 'USD',
        'cik': '0000320193',
        'isin': 'US0378331005',
        'cusip': '037833100',
        'isFund': False}

code

pd.DataFrame([data])

result

    symbol  price   beta    volAvg  mktCap  lastDiv range   changes companyName currency    cik isin    cusip   isFund
0   AAPL    150.72  1.246644    89576498    2397668846469   0.91    129.04-182.94   1.93    Apple Inc.  USD 0000320193  US0378331005    037833100   False
  • Related