首页 > 解决方案 > 使用类的属性作为列的标题,从类的对象创建一个 DataFrame

问题描述

我有这个类项目,我很想将其转换为 DataFrame

projects = [(Project){
   id = 21
   UserDefinedFields = ""
   ProjectName = "Connecting Python Script to AutoTask"
   AccountID = 0
   Type = 4
   ExtPNumber = ""
   ProjectNumber = "P20200311.0001"
   Description = "This project is to test connection between a script and the CRM platform of AutoTask"
   CreateDateTime = 2020-03-11 04:00:00+00:00
   CreatorResourceID = 29684475
   StartDateTime = 2020-03-11 04:00:00+00:00
   EndDateTime = 2020-03-12 04:00:00+00:00
   Status = 1
   ProjectLeadResourceID = 29684475
   CompletedPercentage = 0
   StatusDetail = ""
   StatusDateTime = 2020-03-11 15:34:50.310000+00:00
   LastActivityResourceID = 29684475
   LastActivityDateTime = 2020-03-11 15:34:50.433000+00:00
   LastActivityPersonType = 1
 }, (Project){
   id = 22
   UserDefinedFields = ""
   ProjectName = "Larry and Brittany Test"
   AccountID = 29683567
   Type = 5
   ExtPNumber = ""
   ProjectNumber = "P20200311.0002"
   Description = ""
   CreateDateTime = 2020-03-11 04:00:00+00:00
   CreatorResourceID = 29684474
   StartDateTime = 2020-03-11 04:00:00+00:00
   EndDateTime = 2020-03-12 04:00:00+00:00
   Duration = 2
   ActualHours = 0.0
   ActualBilledHours = 0.0
   EstimatedTime = 0.0
   LaborEstimatedRevenue = 0.0
   LaborEstimatedCosts = 0.0
   LaborEstimatedMarginPercentage = 0.0
   ProjectCostsRevenue = 0.0
   ProjectCostsBudget = 0.0
   ProjectCostEstimatedMarginPercentage = 0.0
   ChangeOrdersRevenue = 0.0
   SGDA = 0.0
   OriginalEstimatedRevenue = 0.0
   EstimatedSalesCost = 0.0
   Status = 1
   ProjectLeadResourceID = 29684474
   CompletedPercentage = 0
   StatusDetail = ""
   StatusDateTime = 2020-03-11 22:08:22.267000+00:00
   PurchaseOrderNumber = ""
   LastActivityResourceID = 29684474
   LastActivityDateTime = 2020-03-11 22:08:22.377000+00:00
   LastActivityPersonType = 1
 }]

id、UserDefinedFields、AccountID 都是类Project的属性

我想生产这样的数据

id | UserDefinedFields | AccountID | ... |LastActivitityPersonType
21 |                   | 0         | ... |
22 |                   | 29683567  |.....| 1

对于没有属性值的对象,数据框应该添加一个空字符串

标签: pythondataframeclass

解决方案


  1. 不修改类:
pd.DataFrame([
  {
    k: v 
    for k, v 
    in project.__dict__.items() 
    if not callable(v)
  }
  for project in projects
])
  1. 添加 to_dict 方法并使用from_records
class Project
   def to_dict(self):
     return {
       attr: getattr(self, attr, '')
       for attr in ['id', 'UserDefinedFields' .... ]  
     }

   ...

pd.Dataframe.from_records(projects)

推荐阅读