Группировка данных из нескольких столбцов во фрейме данных в сводный вид - PullRequest
0 голосов
/ 31 января 2019

У меня есть фрейм данных, как показано ниже, и я хотел бы создать сводную информацию, как показано.Не могли бы вы помочь, как это можно сделать в пандах.

Фрейм данных:

import pandas as pd

ds = pd.DataFrame(

   [{"id":"1","owner":"A","delivery":"1-Jan","priority":"High","exception":"No Bill"},{"id":"2","owner":"A","delivery":"2-Jan","priority":"Medium","exception":""},{"id":"3","owner":"B","delivery":"1-Jan","priority":"High","exception":"No Bill"},{"id":"4","owner":"B","delivery":"1-Jan","priority":"High","exception":"No Bill"},{"id":"5","owner":"C","delivery":"1-Jan","priority":"High","exception":""},{"id":"6","owner":"C","delivery":"2-Jan","priority":"High","exception":""},{"id":"7","owner":"C","delivery":"","priority":"High","exception":""}]

)

Data Frame

Результат:

Summary Data

1 Ответ

0 голосов
/ 31 января 2019

Использование:

#crosstab and rename empty string column
df = pd.crosstab(ds['owner'], ds['delivery']).rename(columns={'':'No delivery Date'})
#change positions of columns - first one to last one
df = df[df.columns[1:].tolist() + df.columns[:1].tolist()]
#get counts by comparing and sum of True values
df['high_count'] = ds['priority'].eq('High').groupby(ds['owner']).sum().astype(int)
df['exception_count'] = ds['exception'].eq('No Bill').groupby(ds['owner']).sum().astype(int)
#convert id to string and join with ,
df['ids'] = ds['id'].astype(str).groupby(ds['owner']).agg(','.join)
#index to column
df = df.reset_index()
#reove index name delivery
df.columns.name = None
print (df)
  owner  1-Jan  2-Jan  No delivery Date  high_count  exception_count    ids
0     A      1      1                 0           1                1    1,2
1     B      2      0                 0           2                2    3,4
2     C      1      1                 1           3                0  5,6,7
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...