I am new to pandas. I need to read a xlsx
file and convert first column to key of a dict and second column to values of a dict using pandas
. I also need to skip / exclude first row which are headers.
The answer here is for pymysql
and here is for csv
. I need to user pandas
.
Here is a sample excel data
dict_key dict_value
key1 str_value1
key2 str_value2
key3 None
key4 int_value3
My code so far is as below.
import pandas as pd
excel_file = "file.xlsx"
xls = pd.ExcelFile(excel_file)
df = xls.parse(xls.sheet_names[0], skiprows=1, index_col=None, na_values=['None'])
data_dict = df.to_dict()
However, it gives me dict where keys are column numbers and values are both column1 data as well as column2 data.
>>> data_dict
{u'Chg_Parms': {0: u' key1 ', 1: u' key2 ', 2: u' key3 ', 3: u' key4 ', 4: u' str_value1 ',
5: u' str_value2 ', 6: u' Nan ', 6: u' int_value3 '}}
what I would like to have is column1 data as key and column two data as values and also NaN
replaced with None
data_dict = {'key1': 'str_value1', 'key2': 'str_value2', 'key3': None, 'key4': int_value3}
Thanks for your help.