lunes, 30 de septiembre de 2024

Pandas cookbook


Read the excel file 

>>> df = pd.read_excel('/home/ambiorixg12/Downloads/500.xlsx')

Save values as list
lst= df.values.tolist()

Count values  from the list 

 len(lst)
500


Print first and last entry
>> print(lst[0],'\n',lst[-1])
['Gaston', 'Willie', 6009.0, nan, 'Howells Ferry', 'Rd', nan, nan, 'Mobile', 'AL', 36618, '(251) 304-1631', 'Male'] 
 ['Paulo', 'Frank', 1801.0, nan, 'Ridgecrest', 'St', nan, nan, 'Valdosta', 'GA', 31601, '(229) 671-9476', 'Male']


Get columns name


headers = df.columns.tolist()

Data type
print(df.dtypes)




-----


Create a dict with the first value and include the column names
God version
 df = pd.read_excel('/home/ambiorixg12/Downloads/500.xlsx')
 c={df.columns.tolist()[a]:df.values.tolist()[0][a] for a in range(len(df.values.tolist()[0])) }


get the first value of are data set
>>> v=[a for a in  df.values.tolist()[0]]  
>>> 
>>> 
>>> 
>>>  ge colum names
>>> h=[a for a in  df.columns.tolist()]


>>>  create are dict
>>> new={h[v.index(a)]:a for a in v}
>>> print(new)
{'Last Name': 'Gaston', 'First Name': 'Willie', 'House Number': 6009.0, 'Pre-directional': nan, 'Street': 'Howells Ferry', 'Street Suffix': 'Rd', 'City': 'Mobile', 'State': 'AL', 'Zip Code': 36618, 'Phone Number': '(251) 304-1631', 'Gender': 'Male'}
>>> 

Or simplify version


>>> new={h[v.index(a)]:a for a in [a for a in  df.values.tolist()[0]]}



---getting header

>>> c={df.columns.tolist()[a] for a in range(len(df.values.tolist()[0])) }
>>> c
{'First Name', 'Gender', 'Pre-directional', 'State', 'House Number', 'Post-directional', 'Last Name', 'Phone Number', 'Street', 'Zip Code', 'City', 'Apartment Number', 'Street Suffix'}
>>> 

info=requests.get('https://ipinfo.io/json').json() 

>>> df = pd.DataFrame.from_dict(info, orient='index', columns=['value'])


No hay comentarios:

Publicar un comentario