【问题标题】:Python pandas get value from dataframe and store in objectPython pandas 从数据框中获取值并存储在对象中
【发布时间】:2020-07-19 15:36:01
【问题描述】:

鉴于以下 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

【问题讨论】:

  • 你能解释一下year must only appear once in the array of objects.吗,因为在2019这一年有不同的条目有不同的ExdDatePayDate
  • 是的,刚刚意识到这没有意义。编辑了问题。希望它现在更有意义。
  • @Sushanth 不,我再次编辑了它

标签: python python-3.x pandas python-3.7


【解决方案1】:

让我们试试这个,Series.between 过滤年份之间的记录,然后是 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%

【讨论】:

    猜你喜欢
    • 2021-07-03
    • 2022-01-19
    • 2010-11-13
    • 2012-12-25
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 2018-01-28
    相关资源
    最近更新 更多