The data source is roughly like this:
Based on this, I came up with an idea: see how to quickly implement this operation in Python.
The data source has been constructed, let’s get started!
import pandas as pd df = pd.read_excel("分列转到行.xlsx",header=None) df.columns = ["年级","姓名"] df
The results are as follows:
The whole code is very simple, take a look if you don’t believe it!
df["新列"] = df["姓名"].str.split(";") df["新列"]
The results are as follows:
Finally add an explosion function and win it directly!
df1 = df.explode("新列") df1
Some screenshots are as follows:
How should you restore this data for the df1 obtained above?
def func(df): return ','.join(df.values) #这里改为什么分隔符,随你自己! df2 = df1.groupby(by='年级').agg(func).reset_index() df2
The results are as follows:
The above is the detailed content of How to efficiently implement column to row operations in Python. For more information, please follow other related articles on the PHP Chinese website!