Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中对该数据帧进行分组?_Python_Pandas_Dataframe_Grouping_Data Manipulation - Fatal编程技术网

如何在python中对该数据帧进行分组?

如何在python中对该数据帧进行分组?,python,pandas,dataframe,grouping,data-manipulation,Python,Pandas,Dataframe,Grouping,Data Manipulation,我有一个问题: import pandas as pd stripline = "----------------------------" rawData = { 'order number': ['11xa', '11xa', '11xa', '21xb', '31xc'], 'working area': ['LLA', 'LLE', 'LLS', 'MLA', 'MLE'], 'time': [1, 6, 13, 35, 24] } df = pd.Data

我有一个问题:

import pandas as pd

stripline = "----------------------------"

rawData = {
    'order number': ['11xa', '11xa', '11xa', '21xb', '31xc'],
    'working area': ['LLA', 'LLE', 'LLS', 'MLA', 'MLE'],
    'time': [1, 6, 13, 35, 24]
}

df = pd.DataFrame(rawData)
print("original data:")
print(df.head())

print(stripline)

rawData2 = {
    'order number': ['11xa', '21xb', '31xc'],
    'working area': ['LLS', 'MLA', 'MLE'],
    'time': [20, 35, 24]
}
df2 = pd.DataFrame(rawData2)

print("expected result:")
print("group after order number, sum all times to that order and choose working field with the biggest time")
print(df2.head())
如何操作数据帧df以获得df2

我想将时间列中与订单号对应的所有值相加。我希望使用时间最长的工作字段,特别是我希望保留其余的数据。新的数据帧有三个顺序,旧的一个有五个

import pandas as pd

rawData = {
    'order_number': ['11xa', '11xa', '11xa', '21xb', '31xc'],
    'working_area': ['LLA', 'LLE', 'LLS', 'MLA', 'MLE'],
    'time': ['1', '6', '13', '35', '24']
    }

df = pd.DataFrame(rawData)
df['time'] = df['time'].apply(int) 
df = df.groupby('order_number', as_index=False).apply(lambda g: g.assign(sum=g.time.sum()))
df.groupby('order_number', ascending=False).apply(lambda x: x.sort_values('time').head(1)).reset_index(drop=True)
给你

  order_number working_area  time  sum
0         11xa          LLA     1   20
1         21xb          MLA    35   35
2         31xc          MLE    24   24
输出

order number    time
0     11xa     20
1     21xb     35
2     31xc     24

尝试这样做,您可以使用
agg
来执行此操作:(注意:列名中的小更改):

输出:

   order_number  time working_area
0         11xa    20          LLS
1         21xb    35          MLA
2         31xc    24          MLE

不,这会给出一个包含两列(工作区)的数据帧。如果我使用df.groupby('ordernumber',)['time'].sum(),我也会遇到同样的问题。请看一看我的印刷品(df2)。这是它的外观。幸运的是,我不清楚,你想要实现什么<代码>'11xa'具有工作区
'LLA'
'LLE'
。所以要么忽略它(第一个代码段),要么考虑它(第二个代码段)。请再次运行我的代码。订单号为11xa。有三个任务对应于该订单号。任务是LLA、LLE和LLS。我总结了与这个顺序对应的所有时间,即20,然后选择时间值最高的工作字段。因此,由于示例中的13分钟,工作字段为LLS。特别是我需要保留其余的数据。请运行演示代码以了解我的意思。更新并更正了解决方案。也许有更聪明的解决方案,但它工作正常。
rawData = {
    'order_number': ['11xa', '11xa', '11xa', '21xb', '31xc'],
    'working_area': ['LLA', 'LLE', 'LLS', 'MLA', 'MLE'],
    'time': ['1', '6', '13', '35', '24']
    }

df = pd.DataFrame(rawData)
df['time'] = df['time'].apply(int)  
df = df.sort_values(['time'], ascending=True)

df = df.groupby(['order_number'], as_index=False).agg(dict(working_area='last', time='sum'))
   order_number  time working_area
0         11xa    20          LLS
1         21xb    35          MLA
2         31xc    24          MLE