Python pandas 从数据框中获取值并存储在对象中
Python pandas get value from dataframe and store in object
给定以下 pandas 数据框,我如何创建一个对象数组,其中包含年份介于 2020 和 2015 之间的行中的所有值?假设数据框中包含 2020 年到 2010 年的数据。
Year Yield Total Amount ExDate PayDate \
0 2020 3.09% SGD 0.66 SGD0.33 2020-05-12 2020-05-26
1 2020 3.09% SGD 0.66 SGD0.33 2020-05-12 2020-05-26
2 2019 7.02% SGD 1.5 SGD0.3 2019-11-18 2019-11-29
3 2019 7.02% SGD 1.5 SGD0.3 2019-08-05 2019-08-20
4 2019 7.02% SGD 1.5 SGD0.3 2019-05-17 2019-05-31
5 2019 7.02% SGD 1.5 SGD0.6 2019-05-02 2019-05-17
6 2018 7.95% SGD 1.7 SGD0.6 2018-08-08 2018-08-21
7 2018 7.95% SGD 1.7 SGD0.6 2018-05-03 2018-05-15
8 2018 7.95% SGD 1.7 SGD0.5 2018-05-03 2018-05-15
9 2017 2.95% SGD 0.63 SGD0.33 2017-08-11 2017-09-27
对象:
class Object:
def__init(self, year, yield_data, amount, total, ex_date, pay_date):
self.year = year
self. yield_data = yield_data
self.total = total
self.ex_date = ex_date
self.pay_date = pay_date
self.amount = amount
让我们试试这个,Series.between
to filter the records between year's followed by DataFrame.drop_duplicates
获取唯一的行,然后是
DataFrame.rename
根据构造函数参数的列,用于稍后在对象构造期间解包。
rename_cols = {'Year': 'year', 'Yield': 'yield_data', 'Amount': 'amount',
'Total': 'total', 'ExDate': 'ex_date', 'PayDate': 'pay_date'}
data = (
df[df.Year.between(2015, 2020)].drop_duplicates(subset=['Year']).
rename(columns=rename_cols).to_dict(orient='records')
)
object_list = [Object(**x) for x in data]
for obj in object_list:
print(f"Year : {obj.year}, Yield {obj.yield_data}")
Year : 2020, Yield 3.09%
Year : 2019, Yield 7.02%
Year : 2018, Yield 7.95%
Year : 2017, Yield 2.95%
给定以下 pandas 数据框,我如何创建一个对象数组,其中包含年份介于 2020 和 2015 之间的行中的所有值?假设数据框中包含 2020 年到 2010 年的数据。
Year Yield Total Amount ExDate PayDate \
0 2020 3.09% SGD 0.66 SGD0.33 2020-05-12 2020-05-26
1 2020 3.09% SGD 0.66 SGD0.33 2020-05-12 2020-05-26
2 2019 7.02% SGD 1.5 SGD0.3 2019-11-18 2019-11-29
3 2019 7.02% SGD 1.5 SGD0.3 2019-08-05 2019-08-20
4 2019 7.02% SGD 1.5 SGD0.3 2019-05-17 2019-05-31
5 2019 7.02% SGD 1.5 SGD0.6 2019-05-02 2019-05-17
6 2018 7.95% SGD 1.7 SGD0.6 2018-08-08 2018-08-21
7 2018 7.95% SGD 1.7 SGD0.6 2018-05-03 2018-05-15
8 2018 7.95% SGD 1.7 SGD0.5 2018-05-03 2018-05-15
9 2017 2.95% SGD 0.63 SGD0.33 2017-08-11 2017-09-27
对象:
class Object:
def__init(self, year, yield_data, amount, total, ex_date, pay_date):
self.year = year
self. yield_data = yield_data
self.total = total
self.ex_date = ex_date
self.pay_date = pay_date
self.amount = amount
让我们试试这个,Series.between
to filter the records between year's followed by DataFrame.drop_duplicates
获取唯一的行,然后是
DataFrame.rename
根据构造函数参数的列,用于稍后在对象构造期间解包。
rename_cols = {'Year': 'year', 'Yield': 'yield_data', 'Amount': 'amount',
'Total': 'total', 'ExDate': 'ex_date', 'PayDate': 'pay_date'}
data = (
df[df.Year.between(2015, 2020)].drop_duplicates(subset=['Year']).
rename(columns=rename_cols).to_dict(orient='records')
)
object_list = [Object(**x) for x in data]
for obj in object_list:
print(f"Year : {obj.year}, Yield {obj.yield_data}")
Year : 2020, Yield 3.09%
Year : 2019, Yield 7.02%
Year : 2018, Yield 7.95%
Year : 2017, Yield 2.95%