如何管理 pandas 数据中的单位? [英] How can I manage units in pandas data?

查看:110
本文介绍了如何管理 pandas 数据中的单位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种很好的方法来管理我的 Pandas 数据中的单位.例如,我有一个 DataFrame 看起来像这样:

I'm trying to figure out if there is a good way to manage units in my pandas data. For example, I have a DataFrame that looks like this:

   length (m)  width (m)  thickness (cm)
0         1.2        3.4             5.6
1         7.8        9.0             1.2
2         3.4        5.6             7.8

目前,度量单位以列名称编码.缺点包括:

Currently, the measurement units are encoded in column names. Downsides include:

  1. 列选择很尴尬——df['width (m)'] vs. df['width']
  2. 如果我的源数据的单位发生变化,事情可能会中断

如果我想从列名中去除单位,还有其他地方可以存储信息吗?

If I wanted to strip the units out of the column names, is there somewhere else that the information could be stored?

推荐答案

目前没有任何好的方法可以做到这一点,请参阅 github issue 此处 进行一些讨论.

There isn't any great way to do this right now, see github issue here for some discussion.

作为一个快速的黑客,可以做这样的事情,用单位维护一个单独的字典.

As a quick hack, could do something like this, maintaining a separate dict with the units.

In [3]: units = {}

In [5]: newcols = []
   ...: for col in df:
   ...:     name, unit = col.split(' ')
   ...:     units[name] = unit
   ...:     newcols.append(name)

In [6]: df.columns = newcols

In [7]: df
Out[7]:
   length  width  thickness
0     1.2    3.4        5.6
1     7.8    9.0        1.2
2     3.4    5.6        7.8

In [8]: units['length']
Out[8]: '(m)'

这篇关于如何管理 pandas 数据中的单位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆