Created
September 20, 2017 21:37
-
-
Save fcojperez/e3c3155ec4d21a91843023ed66141524 to your computer and use it in GitHub Desktop.
Generating a dictionary from DataFrame string dictionary key column with values key joined by token and DataFrame string value column
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################## | |
# Script for generating a Dictionary splitting DataFrame values | |
################################## | |
# Programador: Francisco Perez | |
# Date: 9/20/2017 (MM/DD/YYYY) | |
# Mail: [email protected] | |
import pandas as pd | |
# Defining public variables | |
attendees = {} | |
def fillDictionary(strKey,value,token,newToken=';'): | |
listKey = strKey.replace(token,newToken).split(newToken) | |
listKey = [x for x in listKey if not(x.isnumeric())] | |
for x in listKey: | |
if x in attendees: | |
attendees[x] = attendees[x] + [value] | |
else: | |
attendees[x] = [value] | |
def start(): | |
p = 'Javier;#34;Carlos;#45;Manuel;#32' | |
df = pd.DataFrame([{'value':'wk1','attendees':'Javier;#3;Jose;#4;Manuel;#45'},{'value':'wk2','attendees':'Jorge;#6;Javier;#45;Manuel;#45'}]) | |
for index,row in df.iterrows(): | |
print(row[1]) | |
fillDictionary(row[0],row[1],';#') | |
print(attendees) | |
if __name__ == '__main__': | |
start() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment