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

cnwbcb6i  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(93)

enter image description here我想采取的车辆类别1-12,车辆制造商和型号的列表,并打印一个12行表格/图表,在每个类行相应的制造商和型号。

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

字符串
尝试了这个:下面的代码将重复的相同的make分组在一行中,对于模型也是一样的,另外当我保存到新的csv时,它不显示class列。

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)


期望输出为:

类|使|模型

| Acura| SLX, TLX

1|福特|融合

| Acura| CSX

二|福特|F150,远征

bnlyeluc

bnlyeluc1#

您可以尝试以下解决方案:

def format_class_column22(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_column22)
# 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"))
# grouping by 'Make' and concatenating the 'Model' values
grouped_make_model = df.groupby('Make')['Model'].apply(lambda x: ','.join(x)).reset_index()
print(tabulate(grouped_make_model, headers="keys", showindex="never", tablefmt="fancy_grid"))

字符串


的数据

相关问题