python - 将列表分配给类实例

标签 python variables object instance-variables

我想要获取各种变量名称的列表,并将它们作为实例变量分配给一个类。

此外,我还想从数据库为这些实例变量分配属性。

例如:我有一个带有标题的数据框('col1','col2','col3','col4')。每一行应该是一个类实例,每一列应该是该类的实例变量。然后,应将每行中的值作为每个类实例的属性分配给每个实例变量。

我怎样才能做到这一点?

这里是变量列表:

Index(['Id', 'MSSubClass', 'MSZoning', 'LotFrontage', 'LotArea', 'Street',
       'Alley', 'LotShape', 'LandContour', 'Utilities', 'LotConfig',
       'LandSlope', 'Neighborhood', 'Condition1', 'Condition2', 'BldgType',
       'HouseStyle', 'OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd',
       'RoofStyle', 'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType',
       'MasVnrArea', 'ExterQual', 'ExterCond', 'Foundation', 'BsmtQual',
       'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinSF1',
       'BsmtFinType2', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'Heating',
       'HeatingQC', 'CentralAir', 'Electrical', '1stFlrSF', '2ndFlrSF',
       'LowQualFinSF', 'GrLivArea', 'BsmtFullBath', 'BsmtHalfBath', 'FullBath',
       'HalfBath', 'BedroomAbvGr', 'KitchenAbvGr', 'KitchenQual',
       'TotRmsAbvGrd', 'Functional', 'Fireplaces', 'FireplaceQu', 'GarageType',
       'GarageYrBlt', 'GarageFinish', 'GarageCars', 'GarageArea', 'GarageQual',
       'GarageCond', 'PavedDrive', 'WoodDeckSF', 'OpenPorchSF',
       'EnclosedPorch', '3SsnPorch', 'ScreenPorch', 'PoolArea', 'PoolQC',
       'Fence', 'MiscFeature', 'MiscVal', 'MoSold', 'YrSold', 'SaleType',
       'SaleCondition', 'SalePrice'],
      dtype='object')

这是一个示例数据框:

import pandas as pd
from numpy import nan
d = {'name' : pd.Series(['steve', 'jeff', 'bob'], index=['1', '2', '3']),
       ....:      'salary' : pd.Series([34, 85, 213], index=['1', '2', '3']), 'male' : pd.Series([1, nan, 0], index=['1', '2', '3']), 'score' : pd.Series([1.46, 0.8, 3.], index=['1', '2', '3'])}

df = pd.DataFrame(d)

最佳答案

这非常适合 namedtuple s。

#! /usr/bin/env python3


import collections
import pandas as pd


if __name__ == '__main__':

    Person = collections.namedtuple('Person', 'male name salary score')

    d = {'name': pd.Series(['steve', 'jeff', 'bob'], index=['1', '2', '3']),
         'salary': pd.Series([34, 85, 213], index=['1', '2', '3']),
         'male': pd.Series([1, float('NaN'), 0], index=['1', '2', '3']),
         'score': pd.Series([1.46, 0.8, 3.], index=['1', '2', '3'])}
    df = pd.DataFrame(d, columns=sorted(d.keys()))
    print(df)

    for row in df.values:
        print(Person(*row.tolist()))

输出:

   male   name  salary  score
1   1.0  steve      34   1.46
2   NaN   jeff      85   0.80
3   0.0    bob     213   3.00
Person(male=1.0, name='steve', salary=34, score=1.46)
Person(male=nan, name='jeff', salary=85, score=0.8)
Person(male=0.0, name='bob', salary=213, score=3.0)

关于python - 将列表分配给类实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46626452/

相关文章:

javascript - 如何在 TypeScript 项目的 JSON 类中重写 JSON.stringify 方法,而不是在自定义调用中?

python - 使用 SAGE 进行二进制字段反转

javascript:从局部变量修改全局变量

java - 如何将变量递增到字母表中的下一个或上一个字母?

javascript - 如何设置对象数组的 redux 初始状态?

Java ArrayList 似乎在 init 方法之后无效

Python:我如何确定 throw 时使球行进最远的角度?

Python - 阶乘之和

python - 游戏错误 : mixer system not initialized

c - gcc:为什么两个不同的变量在调用后具有相同的值?