单元格多个数据分列后添加行
表格中单元格包含多个数据,需要将单元格内的多个数据分列后添加到行数据。示例如下:
处理前表格:
目标是对选项列中分号分隔的数据进行处理,并添加行数据。
处理后表格:
点击查看代码
#单元格多个数据分列后添加行
import pandas as pd
import numpy as np
split_column_name = input("请输入需要分解的列名:")
split_symbol = input("请输入分列符号:")
#读取数据
data_info = pd.read_excel(r"C:/Users/lenovo/Desktop/单元格多个数据分列添加行.xlsx",
#sheet_name = "sheet1",
header = 0)
print(data_info)
#对指定列进行分列,分隔符号为“;”
#data_split_info = data_info["选项"].str.split(";",expand = True)
data_split_info = data_info[split_column_name].str.split(split_symbol,expand = True)
print(data_split_info)
print("="*20)
#对分列数据进行横向拼接
df_new = pd.DataFrame(data_split_info.to_numpy().reshape(-1, 1, order='C'),
columns=[0]) #横向拼接
print(df_new)
print("="*20)
#复制数据行,扩充表格
newdf = pd.DataFrame(np.repeat(data_info.values,len(data_split_info.columns),axis=0))
newdf.columns = data_info.columns
print(newdf)
print("="*20)
#插入横向拼接的分列数据
newdf.insert(loc=2,column = split_column_name+'1',value=df_new[[0]])
print(newdf)
print("="*20)
def del_null(x):
return (x is not None)
#return (x is not np.nan)("wp" in x)
#("wp" in x) or
newdf1 = newdf.loc[newdf[split_column_name+'1'].apply(del_null)]
print(newdf1)