Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method to split multiple lines and merge multiple lines in Python?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to split multiple lines in Python" and "what is the method of combining multiple lines with one line". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Mention the following problem, one of which is "one line is split into multiple lines", and the other is "multiple lines and one line". It seems that the group friends have solved it with power query. But what do you do based on Python? Keep looking down.

Split multiple lines in one line

I will provide two ideas for you to choose from on the above question. Of course, the simpler the better. There are some useful skills in each method. I hope you can learn it well.

1) method 1

There are many important knowledge points in the code below, we need to go down and study hard, I only provide ideas for solving problems, about how to use each knowledge point, I hope you can go down and study by yourself.

The usage of Pandas.melt () function

The usage of the parameter expand=True in Series.str.split ("/", expand=True)

Series.sort_values () sorts the text

The usage of enumerate () function in Python

Import pandas as pd # read data df = pd.read_excel ("test1.xlsx", sheet_name= "Sheet1") # split a column into multiple columns of df [["Type 1", "Type 2", "Type 3"]] = df ["Movie Type"] .str.split ("/", expand=True) # Select the desired column df_final = df [["Movie name", "Type 1", "Type 2" "Type 3"] # Line transfer column df_final = df_final.melt (id_vars= ["movie name"], value_name= "type") # sort the "movie name" field df_final = df_final [["movie name", "type"]] df_final.sort_values (by= "movie name", inplace=True) # delete the line for index of "type = None" Value in enumerate (df_final ["type"]): if value = = None: df_final.drop (df_ final.index [index], inplace=True) df_final

The results are as follows:

2) method 2

The above method is really complicated, and since my Pandas version is 0.23.4, I can't use the explode () method to crack. In the pandas0.25 version, a new explode () method was added to DataFrame to change one line into multiple lines.

The usage of Pandas.explode () function

Import pandas as pd # read data df = pd.read_excel ("test1.xlsx", sheet_name= "Sheet1") # split a line into a list. Note: there is no need to use the expand=True parameter df ["type"] = df ["Movie Type"] .str.split ("/") # directly split the specified column df.explode ("type")

The results are as follows:

Multi-line and line-by-line there is no special knowledge used here. A good understanding of the application of a function in grouping aggregation in Pandas can easily solve this problem.

Import pandas as pd # read data df = pd.read_excel ("test1.xlsx", sheet_name= "Sheet2") # grouping aggregate, apply a function def func (df): return', '.join (df.values) df = df.groupby (by=' movie name') .agg (func). Reset_index () df

The results are as follows:

This is the end of the content of "how to split multiple lines in one line in Python" and "what is the method of combining multiple lines with one line". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report