Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use pandas to merge multiple lines in python

Shulou Source: shulou.com Published: 2022-06-02 07:59:32 10月22日 Update

This article is about how to use pandas to merge multiple lines in python, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

There is a need to append the original value after summing the column sum. This requires a multi-line merge.

After grouping according to name, sum and add a column to show the original value

# define functions to separate merged data with commas

> > def joinValues (df):

Return', '.join (df.astype (' str'))

# use the agg function to sum and merge separately

> ds = df.groupby (by='name'). Agg ({'sum', joinValues: [' sum', joinValues]})

> ds

C

Sum joinValues

Name

Lb 88 88

Lc 91 45,23,23

# reset index

> > ds.reset_index ()

Name C

Sum joinValues

0 lb 88 88

1 lc 91 45,23,23

#

Ds.to_excel (ringing DVOGRAPHY excel Universe res.xlsx')

The results are as follows:

The above is how to use pandas to merge multiple lines in python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Multi-line original function more knowledge article practical that is work meeting data article look knowledge point result industry details information channel follow comma Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi Xiaomi MySQL Shulou Technology