JosephZYU
11/8/2019 - 4:43 PM

map / lists / dictionaries

import pandas as pd
import numpy as np

data = {'account': ['Jones LLC', 'Alpha Co', 'Blue Inc', 'Mega Corp'], 
        'Total Sales': [150, 200, 75, 300], 
        'Country': ['US', 'UK', 'US', 'US']}
df = pd.DataFrame.from_dict(data)
#* Converters and Options

import xlwings as xw
import numpy as np
import pandas as pd
import datetime as dt

wb = xw.Book()
sht = wb.sheets[0]

#* Dictionary Converter

# Lets write out some data
sht.range('A8').value = [['a', 1], ['b', 2]]

sht.range('A8:B9').options(dict).value
import pandas as pd
import numpy as np
df = pd.read_csv(
    'https://data.cityofnewyork.us/api/views/vfnx-vebw/rows.csv?accessType=DOWNLOAD&bom=true&format=true'
)
Build a mapping list so we can see the index of all the columns

col_mapping = [f"{c[0]}:{c[1]}" for c in enumerate(df.columns)]
col_mapping
['0:X',
 '1:Y',
 '2:Unique Squirrel ID',
 '3:Hectare',
 '4:Shift',
 '5:Date',
 '6:Hectare Squirrel Number',
 '7:Age',
 '8:Primary Fur Color',
 '9:Highlight Fur Color',
 '10:Combination of Primary and Highlight Color',
 '11:Color notes',
 '12:Location',
 '13:Above Ground Sighter Measurement',
 '14:Specific Location',
 '15:Running',
 '16:Chasing',
 '17:Climbing',
 '18:Eating',
 '19:Foraging',
 '20:Other Activities',
 '21:Kuks',
 '22:Quaas',
 '23:Moans',
 '24:Tail flags',
 '25:Tail twitches',
 '26:Approaches',
 '27:Indifferent',
 '28:Runs from',
 '29:Other Interactions',
 '30:Lat/Long',
 '31:Zip Codes',
 '32:Community Districts',
 '33:Borough Boundaries',
 '34:City Council Districts',
 '35:Police Precincts']
We can also build a dictionary

col_mapping_dict = {c[0]:c[1] for c in enumerate(df.columns)}
col_mapping_dict
{0: 'X',
 1: 'Y',
 2: 'Unique Squirrel ID',
 3: 'Hectare',
 4: 'Shift',
 5: 'Date',
 6: 'Hectare Squirrel Number',
 7: 'Age',
 8: 'Primary Fur Color',
 9: 'Highlight Fur Color',
 10: 'Combination of Primary and Highlight Color',
 11: 'Color notes',
 12: 'Location',
 13: 'Above Ground Sighter Measurement',
 14: 'Specific Location',
 15: 'Running',
 16: 'Chasing',
 17: 'Climbing',
 18: 'Eating',
 19: 'Foraging',
 20: 'Other Activities',
 21: 'Kuks',
 22: 'Quaas',
 23: 'Moans',
 24: 'Tail flags',
 25: 'Tail twitches',
 26: 'Approaches',
 27: 'Indifferent',
 28: 'Runs from',
 29: 'Other Interactions',
 30: 'Lat/Long',
 31: 'Zip Codes',
 32: 'Community Districts',
 33: 'Borough Boundaries',
 34: 'City Council Districts',
 35: 'Police Precincts'}