【问题标题】:Pandas: find all unique values in one column and normalize all values in another column to their last valuePandas:查找一列中的所有唯一值并将另一列中的所有值标准化为最后一个值
【发布时间】:2017-12-24 10:21:32
【问题描述】:

问题

我想在一列中查找所有唯一值并规范化 另一列中的值与其最后一个值对应。我想通过 pandas 模块使用 python3 来实现这一点。


示例:

原始数据集

Fruit   | Amount
Orange  |     90
Orange  |     80
Orange  |     10
Apple   |    100
Apple   |     50
Orange  |     20
Orange  |     60   --> latest value of Orange. Use to normalize Orange
Apple   |     75
Apple   |     25
Apple   |     40   --> latest value of Apple. Used to normalize Apple


期望的输出

“水果”列中唯一值的标准化值的比率列

Fruit   | Amount |  Ratio
Orange  |     90 |  90/60 --> 150%  
Orange  |     80 |  80/60 --> 133.3%
Orange  |     10 |  10/60 --> 16.7%
Apple   |    100 | 100/40 --> 250%
Apple   |     50 |  50/40 --> 125%
Orange  |     20 |  20/60 --> 33.3%
Orange  |     60 |  60/60 --> 100%
Apple   |     75 |  75/40 --> 187.5%
Apple   |     25 |  25/40 --> 62.5%
Apple   |     40 |  40/40 --> 100%


Python 代码尝试

import pandas as pd

filename = r'C:\fruitdata.dat'
df = pd.read_csv(filename, delimiter='|')
print(df)
print(df.loc[df['Fruit   '] == 'Orange  '])
print(df[df['Fruit   '] == 'Orange  '].tail(1))


Python 输出 (IPython)

In [1]: df
   Fruit      Amount
0  Orange         90
1  Orange         80
2  Orange         10
3  Apple         100
4  Apple          50
5  Orange         20
6  Orange         60
7  Apple          75
8  Apple          25
9  Apple          40

In [2]: df.loc[df['Fruit   '] == 'Orange  ']
   Fruit      Amount
0  Orange         90
1  Orange         80
2  Orange         10
5  Orange         20
6  Orange         60

In [3]: df[df['Fruit   '] == 'Orange  '].tail(1)
Out[3]: 
   Fruit      Amount
6  Orange         60


问题

如何遍历“Fruit”中的每个唯一项目并根据其规范化所有值 尾值?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您可以将iloc 与groupby 一起使用

    df.groupby('Fruit').Amount.apply(lambda x: x/x.iloc[-1])
    Out[38]: 
    0    1.500000
    1    1.333333
    2    0.166667
    3    2.500000
    4    1.250000
    5    0.333333
    6    1.000000
    7    1.875000
    8    0.625000
    9    1.000000
    Name: Amount, dtype: float64
    

    分配回来后

    df['New']=df.groupby('Fruit').Amount.apply(lambda x: x/x.iloc[-1])
    df
    Out[40]: 
        Fruit  Amount       New
    0  Orange      90  1.500000
    1  Orange      80  1.333333
    2  Orange      10  0.166667
    3   Apple     100  2.500000
    4   Apple      50  1.250000
    5  Orange      20  0.333333
    6  Orange      60  1.000000
    7   Apple      75  1.875000
    8   Apple      25  0.625000
    9   Apple      40  1.000000
    

    不使用lambda

    df.Amount/df.groupby('Fruit',sort=False).Amount.transform('last')
    Out[46]: 
    0    1.500000
    1    1.333333
    2    0.166667
    3    2.500000
    4    1.250000
    5    0.333333
    6    1.000000
    7    1.875000
    8    0.625000
    9    1.000000
    Name: Amount, dtype: float64
    

    【讨论】:

    • 太棒了!你知道我是否可以在没有 lambda 的情况下达到同样的效果吗?
    • @JodyK 让我提供
    • 两个很棒的解决方案!非常感谢。
    • @JodyK 如果它对你有用,别忘了接受它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    相关资源
    最近更新 更多