【发布时间】:2018-03-19 11:35:13
【问题描述】:
我想获取各种变量名的列表,并将它们作为实例变量分配给一个类。
此外,我还想从数据库中为这些实例变量分配属性。
例如:我有一个带有标题的数据框,('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)
【问题讨论】:
-
这几乎是这个问答的副本:stackoverflow.com/questions/1639174/…
-
在这篇文章中,“对象”是从数据框自动创建的。而不必单独定义每个对象。例如:
>>> class AllMyFields: ... def __init__(self, dictionary): ... for k, v in dictionary.items(): ... setattr(self, k, v) ... >>> o = AllMyFields({'a': 1, 'b': 2}) >>> o.a 1必须将对象命名为“0”我希望这些对象成为我可以随意调用的索引
标签: python variables object instance-variables