I am trying to take attributes from a list of objects and create a dataframe with the results... the following process works for the most part, but it seems inefficient and not proper. Is there another approach that wont take so many lines of code?
Below, I am creating blank lists for each column, grabbing an attribute from the object, appending it to the appropriate list, creating a blank dataframe, and mapping the lists to columns. I was hoping to somehow create a loop or use dictionaries or grab multiple columns at once or something
Object format: https://developers.asana.com/docs/tasks
gids = []
task_names = []
custom_fields = [] #status, workstream, subworkstream
custom_values = []
types = []
due_dates = []
portfolios = []
projects = []
sections = []
section_dates = []
assignees = []
parents = []
releases = []
tags = []
task_list = []
tasks = client.tasks.get_tasks_for_project(project, {'param': 'value', 'param': 'value'}, opt_pretty=True)
for x in tasks:
task_list.append(x['gid'])
for task in task_list:
t = client.tasks.get_task(task, {'param': 'value', 'param': 'value'}, opt_pretty=True)
try:
gids.append(t['gid'])
task_names.append(t['name'])
types.append(t['resource_subtype'])
due_dates.append(t['due_on'])
parents.append(t['parent']['name'])
except:
gids.append('None')
task_names.append('None')
types.append('None')
due_dates.append('None')
parents.append('None')
pass
task_df = pd.DataFrame()
task_df['ID'] = gids
task_df['Task'] = task_names
task_df['Type'] = types
task_df['Due Date'] = due_dates
task_df['Parent'] = parents
CodePudding user response:
Given your example, and without benefit of testing, since I don't have the library you are using for tasks, I believe you should rethink you approach as follows:
Rather than all the individual assignment statements, I would create two dictionaries: The first entitled attrib_dict maps the task attrib to a df column heading. The second collects the attribute values for the task of places a NaN value in the position if no attribute exists.
Given these two concepts this is how I would do this task>
import pandas as pd
import numpy as np
#Return a Formatted Dataframe from a list of tasks
def buildDF(tasks):
# maps task attribute to df column head
atrib_dict = {'guid': 'ID', 'name': 'Task', 'resource_subtype': 'Type', 'due_on': 'Due Date', 'parent':' Parent'}
# Dictionary to hold task data according to desired DF format
data_dict = {'ID':[], 'Task':[], 'Type':[], 'Due Date':[], 'Parent':[], '':[], }
# Fetch the tasks to operate upon
atrib_keys = atrib.dict/keys()
for t in tasks:
for atrib in atrib_keys:
try:
val = t[atrib]
except:
val = np.nan
data_dict[atrib_dict[atrib]].append(val)
return pd.DataFrame(data_dict)
Then I would call the buildDf function as follows:
buildDF(client.tasks.get_tasks_for_project(project, {'param': 'value', 'param': 'value'}, opt_pretty=True))