Python Pandas,从.groupby()。apply()中的组中切片行 - python

我有以下代码设置调用和groupBy并应用于Python Pandas DataFrame。

奇怪的是,如果不完全固定输出(如调试所示),我将无法按行对分组的数据进行切片(如df.loc[2:5]所示),如何删除行并获取所需的输出呢?

任何帮助将不胜感激,我正在一个具有更复杂功能的更大示例上运行此操作,但已将问题定位到行切片!

码:

import pandas as pd
df = pd.DataFrame({'one' : ['AAL', 'AAL', 'AAPL', 'AAPL'], 'two' : [1, 2, 3, 4]})

def net_func(df):
    df_res = daily_func(df, True)
    df_res_valid = daily_func(df, False)
    df_merge = pd.merge(df_res, df_res_valid)
    return df_merge

def daily_func(df, bool_param):

#     df.drop(df.head(1).index, inplace=True)
#     df = df[1:1]
#     df.iloc[1:1,:]
#     df.loc[1:1,:]


    if bool_param:
        df['daily'+str(bool_param)] = 1
    else:
        df['daily'+str(bool_param)] = 0    
    return df

print df.groupby('one').apply(net_func)

电流输出:

         one  two  dailyTrue  dailyFalse
one                                     
AAL  0   AAL    1          1           0
     1   AAL    2          1           0
AAPL 0  AAPL    1          1           0
     1  AAPL    2          1           0

所需的输出:

         one  two  dailyTrue  dailyFalse
one                                     
AAL  1   AAL    2          1           0
AAPL 1  AAPL    2          1           0

理想情况下,我希望能够按组逐个切片,例如df.loc[3:5]-太完美了!

我尝试了如下评论:

df.drop(df.head(1).index, inplace=True)输出:

Empty DataFrame
Columns: [one, two, dailyTrue, dailyFalse]
Index: []

更新:还尝试使用df = df[1:1]输出:

Empty DataFrame
Columns: [one, two, dailyTrue, dailyFalse]
Index: []

更新还尝试了df.iloc[1:1,:]

         one  two  dailyTrue  dailyFalse
one                                     
AAL  0   AAL    1          1           0
     1   AAL    2          1           0
AAPL 0  AAPL    1          1           0
     1  AAPL    2          1           0

df.loc[1:1,:]

         one  two  dailyTrue  dailyFalse
one                                     
AAL  0   AAL    1          1           0
     1   AAL    2          1           0
AAPL 0  AAPL    1          1           0
     1  AAPL    2          1           0

参考方案

考虑在xs之后使用横截面切片groupby().apply(),并相应地指定每个键:

print df.groupby('one').apply(net_func).xs(0, level=1)
#       one  two  dailyTrue  dailyFalse
#one                                   
#AAL    AAL    1          1           0
#AAPL  AAPL    1          1           0

print df.groupby('one').apply(net_func).xs(1, level=1)
#       one  two  dailyTrue  dailyFalse
#one                                   
#AAL    AAL    2          1           0
#AAPL  AAPL    2          1           0

或者,将multiple indexing与元组列表一起使用:

print df.groupby('one').apply(net_func).ix[[('AAL', 1), ('AAPL', 1)]]
#         one  two  dailyTrue  dailyFalse
#one                                     
#AAL  1   AAL    2          1           0
#AAPL 1  AAPL    2          1           0

切片功能(在熊猫0.14中引入):

print df.groupby('one').apply(net_func).loc[(slice('AAL','AAPL'),slice(1,1)),:]
#         one  two  dailyTrue  dailyFalse
#one                                     
#AAL  1   AAL    2          1           0
#AAPL 1  AAPL    2          1           0

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…

TypeError:'str'对象不支持项目分配,带有json文件的python - python

以下是我的代码import json with open('johns.json', 'r') as q: l = q.read() data = json.loads(l) data['john'] = '{}' data['john']['use…

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…