How can I manage units in pandas data?

后端 未结 2 572
猫巷女王i
猫巷女王i 2021-02-05 05:24

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:

          


        
2条回答
  •  终归单人心
    2021-02-05 06:16

    As I was searching for this, too. Here is what pint and the (experimental) pint_pandas is capable of today:

    import pandas as pd
    import pint
    import pint_pandas
    
    ureg = pint.UnitRegistry()
    ureg.Unit.default_format = "~P"
    pint_pandas.PintType.ureg.default_format = "~P"
    
    df = pd.DataFrame({
        "length": pd.Series([1.2, 7.8, 3.4], dtype="pint[m]"),
        "width": pd.Series([3.4, 9.0, 5.6], dtype="pint[m]"),
        "thickness": pd.Series([5.6, 1.2, 7.8], dtype="pint[cm]"),
    })
    
    print(df.pint.dequantify())
    
         length width thickness
    unit      m     m        cm
    0       1.2   3.4       5.6
    1       7.8   9.0       1.2
    2       3.4   5.6       7.8
    
    df['width'] = df['width'].pint.to("inch")
    
    print(df.pint.dequantify())
    
         length       width thickness
    unit      m          in        cm
    0       1.2  133.858268       5.6
    1       7.8  354.330709       1.2
    2       3.4  220.472441       7.8
    

提交回复
热议问题