按 Pandas Dataframe 中 3 列中的 2 列进行分组

问题描述 投票:0回答:1

enter image description here我想要获取 1-12 类车辆、车辆品牌和型号的列表,并打印 12 行表格/图表,每个类行中各自的品牌和型号。像这样的东西:

Class | Make  | Model
________________________
      | Acura |SLX
1     | Acura |TLX
      | Ford  |Fusion
_________________________
      | Acura |CSX
2     | Ford  |F150
      | Ford  |Expedition

尝试过这个: 下面的代码将重复的相同品牌连续分组,模型相同,而且当我将其保存到新的 csv 时,它不会显示类列。

import pandas as pd
import numpy as np
from tabulate import tabulate

df = pd.read_excel('data.xlsx')
#df_size = df.shape
df2 = df.drop(['VIN', 'Class Guide Line Item #', 'Asset Type', 'Year', 'Trim','Trim Option 1', 'Trim Option 2','Trim Option 3','Trim Option 4'], axis='columns')
df2.drop_duplicates(inplace=True)
#print(tabulate(df2, headers="keys", showindex="never", tablefmt="fancy_grid"))
df2.head()
df3.groupby(['Class']).agg(list)
df3.groupby(['Class']).agg(pd.Series.tolist)

#df3 = df2.groupby(['Class']).agg(lambda x: list(x))
#print(tabulate(df3, headers="keys", showindex="never", tablefmt="grid"))
df3.head()
#df3.to_csv('newdata.csv', index=False)
python pandas dataframe group-by charts
1个回答
0
投票

欢迎来到 stackoverflow )这个怎么样?

def format_class_column(group):
    group['Class'] = [''] * len(group)
    group.iloc[0, group.columns.get_loc('Class')] = group.name
    return group

# applying the formatting function to each group
df = df.groupby('Class').apply(format_class_column)
# resetting the index to remove the multi-level index created by groupby
df.reset_index(drop=True, inplace=True)
print(tabulate(df, headers="keys", showindex="never", tablefmt="fancy_grid"))

© www.soinside.com 2019 - 2024. All rights reserved.