【问题标题】:How to access the nominal values and uncertainties in a Pandas DataFrame?如何访问 Pandas DataFrame 中的标称值和不确定性?
【发布时间】:2021-12-09 07:45:57
【问题描述】:

我正在使用 uncertainties 模块和 Pandas。目前,我能够将具有不确定性的数据框一起输出到电子表格中。我的主要目标是在相邻列中写入具有不确定性的数据框。但是如何访问数据框中的标称值或不确定性。下面给出了 MWE。

当前输出

A B
63.2+/-0.9 75.4+/-0.9
41.94+/-0.05 53.12+/-0.21
4.1+/-0.4 89.51+/-0.32
28.2+/-0.5 10.6+/-0.6
25.8+/-0.9 39.03+/-0.08
27.26+/-0.09 44.61+/-0.35
25.04+/-0.13 37.7+/-0.6
2.4+/-0.5 50.0+/-0.8
0.92+/-0.21 3.1+/-0.5
57.69+/-0.34 21.8+/-0.8

期望的输出

A +/- B +/-
63.2 0.9 75.4 0.9
41.94 0.05 53.12 0.21
4.1 0.4 89.51 0.32
28.2 0.5 10.6 0.6
25.8 0.9 39.03 0.08
27.26 0.09 44.61 0.35
25.04 0.13 37.7 0.6
2.4 0.5 50 0.8
0.92 0.21 3.1 0.5
57.69 0.34 21.8 0.8

MWE

from uncertainties import unumpy
import pandas as pd
import numpy as np


A_n = 100 * np.random.rand(10)
A_s = np.random.rand(10)

B_n = 100 * np.random.rand(10)
B_s = np.random.rand(10)

AB = pd.DataFrame({'A':unumpy.uarray(A_n, A_s), 'B': unumpy.uarray(B_n, B_s)})


AB_writer = pd.ExcelWriter('A.xlsx', engine = 'xlsxwriter', options={'strings_to_numbers': True})
AB.to_excel(AB_writer, sheet_name = 'Data', index=False, na_rep='nan')
AB_writer.close()

更新

我忘了提到 AB 不是如 MWE 中所示创建的,而是 MWE 中未给出的先前计算的结果。为了 MWE,我创建了 AB。所以简而言之,我将无法访问 A 和 B 标称值和不确定值。

【问题讨论】:

  • 如果您无法访问 A 和 B 标称值和不确定值,您如何处理这两列并对其进行转换?或者,您只有它们的组合值,例如63.2+/-0.9 在文本中?
  • 我只有它们的组合值(经过多次计算)。

标签: python pandas uncertainty


【解决方案1】:

只需将它们分成不同的列:

Au = unumpy.uarray(A_n, A_s)
Bu = unumpy.uarray(B_n, B_s)
AB = pd.DataFrame({'A': unumpy.nominal_values(Au), 'A+/-': unumpy.std_devs(Au), 'B': unumpy.nominal_values(Bu), 'B+/-': unumpy.std_devs(Bu)})

【讨论】:

    【解决方案2】:

    您可以映射列以获得所需的结果。以下代码映射A 列(确保不要将两列分配给同一个列键'+/-'

    AB[['A', '+/-']] = AB.A.apply(lambda x: str(x).split('+/-')).to_list()
    

    【讨论】:

      【解决方案3】:

      您可以使用str.split()将每一列拆分为一列主要值和一列不确定性,如下:

      # add the column labels here if you have more columns to process
      # e.g. `for col in AB[['A', 'B', 'C']]:` if you want to process columns `A`, `B` and `C`
      for col in AB[['A', 'B']]:     
          AB[[col, f'{col}+/-']] = AB[col].str.split(r'\+/-', expand=True)
      
      # sort the columns to put the related columns together
      AB = AB.sort_index(axis=1)    
      

      不建议在同一个数据框中有 2 列相同的列标签。在这里,我们将+/- 列与它们各自的源列名称一起命名,以便区分它们。

      这里,我们还使用.sort_index()对列名进行排序,使相关列彼此相邻。

      结果:

      print(AB)
      
             A  A+/-      B  B+/-
      0   63.2   0.9   75.4   0.9
      1  41.94  0.05  53.12  0.21
      2    4.1   0.4  89.51  0.32
      3   28.2   0.5   10.6   0.6
      4   25.8   0.9  39.03  0.08
      5  27.26  0.09  44.61  0.35
      6  25.04  0.13   37.7   0.6
      7    2.4   0.5   50.0   0.8
      8   0.92  0.21    3.1   0.5
      9  57.69  0.34   21.8   0.8
      

      【讨论】:

      • 我收到错误ValueError: Columns must be same length as key
      • @TomKurushingal 您是否有一些列没有+/- 符号?如果是,请仅遍历具有+/- 符号的列,正如我在代码评论中提到的那样。例如。 for col in AB[['A', 'B']] 仅适用于带有 +/- 符号的 AB
      • @TomKurushingal 第一部分中的循环是为了帮助您自动循环使用+/- 符号的所有列。当然,如果您只有 1 或 2 列,您可以手动执行此操作,例如AB[['A', 'A+/-']] = AB['A'].str.split(r'\+/-', expand=True)AB[['B', 'B+/-']] = AB['B'].str.split(r'\+/-', expand=True) 一次一列。
      • @TomKurushingal 我已经编辑了我上面的代码,以便仅处理示例数据中的 AB 列。如果您有其他列要处理,只需将其添加到列列表中。例如如果要处理列ABC,请使用for col in AB[['A', 'B', 'C']]: 作为第一行代码
      • @TomKurushingal 该解决方案对您有何帮助?请指教。谢谢!
      猜你喜欢
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2013-09-10
      相关资源
      最近更新 更多