Home > Mobile >  Pandas Column 0,1 into Key:Value respectively
Pandas Column 0,1 into Key:Value respectively

Time:12-21

I have a pandas DataFrame similar to this:

     0            1
0  Index       S&P 500
1  MarketCap   1712
2  Income      26
3  Sales       442

My end goal is to have Column 0 to be the keys in a dictionary and then Column 1 to the values (e.g. dict = {"Index": "S&P500", "MarketCap: "1712"}

Any help is greatly appreciated, I am quite new to Pandas/Python and have been referencing this doc with no progress.

Here is the code I have at the moment (which is scraping the data):

import pandas as pd
import requests


BASE_URL = "https://finviz.com/quote.ashx?t=AMZN"
HEADER = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"}

response = requests.get(BASE_URL, headers=HEADER)

result = pd.read_html(response.text)
parsed = result[5]

CodePudding user response:

just do:

dict(df.values)

Example:

df = pd.DataFrame(data={0: ["Index", "MarketCap", "Income", "Sales"], 1:["S&P 500", "1712", "26", "442"]})
dd = dict(df.values)

df:

           0        1
0      Index  S&P 500
1  MarketCap     1712
2     Income       26
3      Sales      442

Output:

{'Index': 'S&P 500', 'MarketCap': '1712', 'Income': '26', 'Sales': '442'}

IN ORDER TO MAKE IT WORK WITH OP DATA:

cols = [0,1]
df = parsed[cols]
dict(df.values)

CodePudding user response:

Use to_dict:

>>> parsed.set_index(0)[1].to_dict()
{'Index': 'S&P 500',
 'Market Cap': '1712.86B',
 'Income': '26.26B',
 'Sales': '457.96B',
 'Book/sh': '237.80',
 'Cash/sh': '156.81',
 'Dividend': '-',
 'Dividend %': '-',
 'Employees': '1335000',
 'Optionable': 'Yes',
 'Shortable': 'Yes',
 'Recom': '1.70'}

CodePudding user response:

You can create a dictionary and iterate over your dataframe columns 0 and 1 and assign 0 values as keys and 1 as values

ans = {}
for idx in parsed.index:
    if parsed[0][idx] not in ans:
        ans[parsed[0][idx]] = parsed[1][idx]
        
print(ans)

Output:

{'Index': 'S&P 500', 'Market Cap': '1712.86B', 'Income': '26.26B', 'Sales': '457.96B', 'Book/sh': '237.80', 'Cash/sh': '156.81', 'Dividend': '-', 'Dividend %': '-', 'Employees': '1335000', 'Optionable': 'Yes', 'Shortable': 'Yes', 'Recom': '1.70'}

CodePudding user response:

We have multiple options to do the same, you can try these two ways.

  1. DataFrame.iterrows()
  2. DataFrame.itertuples()

Example:

 import pandas as pd
 inp = [{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,'c2':120}]
 df = pd.DataFrame(inp)
 print (df)

 #With iterrows method 

 for index, row in df.iterrows():
     print(row["c1"], row["c2"])

 #With itertuples method

 for row in df.itertuples(index=True, name='Pandas'):
     print(row.c1, row.c2)

Now here you can use values to create Dict or List as per your requirement.

Note: itertuples() is supposed to be faster than iterrows()

  • Related