从熊猫数据帧创建字典

时间:2021-03-30 14:44:57

标签: python-3.x excel pandas

我有一个这种格式的 Excel 文件。

site | matrix | sampling type | sampling frequency | measurands
S1   | water  | spot check    | 28 days            | nitrate
S1   | water  | spot check    | 28 days            | pH
S1   | water  | spot check    | 28 days            | conductivity
S1   | mud    | spot check    | 56 days            | phosphate
S1   | water  | mixed sample  | 14 days            | nitrate
S1   | water  | mixed sample  | 14 days            | pH
...

我的目标是从具有以下形式的 Pandas 数据帧创建一个 python 字典(以及后来的 yaml 文件)。

site: S1
matrices:
  - water
    sampling types:
      - spot check
        sampling frequency: 28
          measurands: [nitrate, pH, conductivity]
      - mixed sample
        sampling frequency: 14
          measurands: [nitrate, pH]
  - mud
    sampling types:
      - spot check
        sampling frequency: 56
          measurands: [phosphate]

我希望我的 yaml 是正确的。基本思想是您可以根据以下条件在一个站点上绘制多个样本:

  • 样本来自哪个矩阵。
  • 以何种方式抽取样本(只需在小溪中放一个桶或在我们的一些过程中取水,例如)。
  • 您访问特定矩阵和抽样类型的频率。

在实验室中,正在分析被测量列表。

正如人们所见,上表没有明确的“主键”。站点、矩阵、采样类型和采样频率的组合应该是唯一的,只有被测量不同。

所以我想我真的不能使用方便的 pandas.to_dict() 函数,当有这样一个键时,它确实有效。

使用 python 并以所描述的 excel 表作为唯一限制的任何想法都将不胜感激。 (我对允许使用的工具和输入的格式无能为力。)

提前致谢

布尼

0 个答案:

没有答案