原始数据:
# python处理excel一列变多列
import pandas as pd
# 读取数据
df = pd.read_excel("学生资料.xlsx")
df.head()
# 数据拆分
def split_func(line):
line["姓名"], line['性别'], line['年龄'], line['城市'] = line['数据'].split(":")
return line
# axis=0,操作时只有第0维的下标变化其他不变。
# axis=1,操作时只有第1维的下标变化其他不变。
# axis=2,操作时只有第2维的下标变化其他不变。
df = df.apply(split_func, axis=1)
# inplace = True:不创建新的对象,直接对原始对象进行修改;
# inplace = False:对数据进行修改,创建并返回新的对象承载其修改结果。
df.head()
df.drop(['数据'], axis=1, inplace=True)
df.head()
# 输出到ex表
df.to_excel("拆分excel.xlsx", index=False)
生成新的excel: