[英]Pandas- Combine multiple rows into a single row and create new columns
I am working with order data using pandas.我正在使用熊猫处理订单数据。 Each order can contain up to two rows (one row for each component of the order, which has a max of two components).
每个订单最多可以包含两行(订单的每个组件一行,最多包含两个组件)。
My goal is to take two rows and turn them into one.我的目标是取两行并将它们合二为一。
example input:示例输入:
Order_Number![]() |
INVENTORY CODE![]() |
description1![]() |
---|---|---|
4304 ![]() |
STDROFENHBSM608.511WH ![]() |
8-1/2 x 11, 60# Soporset Digital HiBright Smooth ![]() |
4304 ![]() |
STNDEN695WOL ![]() |
6 x 9.5 DBL WDW ENVELOPE ![]() |
example output:示例输出:
Order_Number![]() |
INVENTORY CODE![]() |
description1![]() |
INVENTORY CODE2![]() |
description2![]() |
---|---|---|---|---|
4304 ![]() |
STDROFENHBSM608.511WH ![]() |
8-1/2 x 11, 60# Soporset Digital HiBright Smooth ![]() |
STNDEN695WOL ![]() |
6 x 9.5 DBL WDW ENVELOPE ![]() |
Here is my current code:这是我当前的代码:
#name of infile to be the name of the file being ingested
infile= ('file1.csv')
infile2= ('file2.csv')
infile3= ('file3.csv')
#length of the infile for naming purposes
size = len(infile)
size2 = len(infile2)
#name outfile to be the name of the desired output file
outfile = (infile[:size -4]+"_"+infile2[:size2 -4]+"_output.csv")
#Data read in as dataframe
df = pd.read_csv(infile, encoding = "ISO-8859-1", engine= 'python')
df2 = pd.read_csv(infile2, encoding = "ISO-8859-1", engine= 'python')
df3 = pd.read_csv(infile3, encoding = "ISO-8859-1", engine= 'python')
df_merge =df.merge(df2, on='Order_Number', how='left')
df_final = pd.merge(df_merge,df3[['PkgID?','Printer', 'Inserter']],on='PkgID?', how='left')
The above code combines the various data I am working with into a single dataframe, but has duplicate rows based off order number as mentioned above.上面的代码将我正在使用的各种数据组合到一个数据框中,但是如上所述,根据订单号有重复的行。
You need to reshape your dataframe.您需要重塑数据框。 This should work
这应该工作
res = pd.DataFrame(df.drop(columns='Order_Number').values.reshape(len(df)//2, 4), # reshape Inventory code and description columns
columns=['INVENTORY CODE', 'description1', 'INVENTORY CODE2', 'description2'], # set new column names
index=df['Order_Number'].drop_duplicates()).reset_index() # set index by Order_Number and reset_index for a new column
res
LIMITATIONS: the separator used here is |
限制:这里使用的分隔符是
|
which mustn't be in your data不能在您的数据中
import pandas as pd
df = pd.read_csv('df.csv')
# combine based on Order_Number
df_ = df.groupby('Order_Number').agg({'INVENTORY CODE':'|'.join,'description':'|'.join}).reset_index()
# split and expand
df_1 = df_['INVENTORY CODE'].str.split('|', expand=True).add_prefix('INVENTORY CODE_')
df_2 = df_['description'].str.split('|', expand=True).add_prefix('description_')
# combine again and remove the ['INVENTORY CODE', 'description'] columns
df_ = pd.concat([df_, df_1, df_2], axis=1).drop(['INVENTORY CODE', 'description'], axis=1)
# sort the columns to match the output
base_cols = [col for col in df_.columns if col.split('_', 1)[0] not in ['INVENTORY CODE', 'description']]
sort_columns = [col for col in df_.columns if col not in base_cols]
df_ = df_[base_cols + sorted(sort_columns, key=lambda x: (x=='Order_Number', int(x.split('_')[-1])))]
output:输出:
Order_Number![]() |
INVENTORY CODE_0![]() |
description_0![]() |
INVENTORY CODE_1![]() |
description_1![]() |
INVENTORY CODE_2![]() |
description_2![]() |
INVENTORY CODE_3![]() |
description_3![]() |
---|---|---|---|---|---|---|---|---|
4304 ![]() |
STDROFENHBSM608.511WH ![]() |
8-1/2 x 11, 60# Soporset Digital HiBright Smooth ![]() |
STNDEN695WOL ![]() |
6 x 9.5 DBL WDW ENVELOPE ![]() |
STNDEN695WOL ![]() |
6 x 9.5 DBL WDW ENVELOPE ![]() |
||
4305 ![]() |
STNDEN695WOL ![]() |
6 x 9.5 DBL WDW ENVELOPE ![]() |
STNDEN695WOL ![]() |
6 x 9.5 DBL WDW ENVELOPE ![]() |
STNDEN695WOL ![]() |
6 x 9.5 DBL WDW ENVELOPE ![]() |
STNDEN695WOL ![]() |
6 x 9.5 DBL WDW ENVELOPE ![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.