在申请中访问分组依据值

时间:2019-12-24 13:37:44

标签: python pandas pandas-groupby

如何从传递给groupby的函数中访问apply的值? 这是一个示例:

import pandas as pd

df = pd.DataFrame(
    [
        ("bird", "Falconiformes", 389.0),
        ("bird", "Psittaciformes", 24.0),
        ("mammal", "Carnivora", 80.2),
        ("mammal", "Primates", 20),
        ("mammal", "Carnivora", 58),
    ],
    index=["falcon", "parrot", "lion", "monkey", "leopard"],
    columns=("class", "order", "max_speed"),
)

class_to_features = {"bird": ["wings", "feathers", "beak"], "mammal": ["udder"]}


def _helper(df):
    # Here I'd like to access the value of the class
    # One option is `print(df.iloc[0]["class"]`
    # but it seems very non-Pythonic
    # return class_to_features[df["class"]]
    print(class_to_features[df["class"].iloc[0]])


df.groupby("class", as_index=False).apply(_helper)

_helper函数中,我想访问groupby字段的值(在本例中为"bird""mammal")。我可以像代码中所示那样进行操作,但是我正在寻找一种更Pythonic或Pandasic的方法。

2 个答案:

答案 0 :(得分:0)

您的数据框如下所示:

          class           order  max_speed
falcon     bird   Falconiformes      389.0
parrot     bird  Psittaciformes       24.0
lion     mammal       Carnivora       80.2
monkey   mammal        Primates       20.0
leopard  mammal       Carnivora       58.0

df.groupby("class").apply()函数访问“类别”列中每个类别(鸟类,哺乳动物)的数据框。

因此,您要应用于分割后的数据帧的函数在_helper中进行了定义。
如果您告诉我您要取得哪种结果,我可以为您提供帮助。

答案 1 :(得分:0)

您可以使用以下命令访问每个groupby对象:

class_to_features = {"bird": ["wings", "feathers", "beak"], "mammal": ["udder"]}

for group_id, group_df in df.groupby("class", as_index=False):
    # Print the features by mapping the dictionary   
    print(class_to_features[group_id])

# You can also map the values from the dictionary directly to the original dataframe:
df['features_list'] = df['class'].map(class_to_features)