#多个单元格合并成一列
def ab(df):
return', '.join(df.values)
#剔除空字符或者空值
data = data[(data['project'].isnull()==False) & (data['project']!='')]
data1 = data.groupby(['id'])['project'].apply(ab)
df = data1.reset_index()
# 一个单元格转成一列
def row_to_col(df,key,value): #key 列名,value 需要转列的字段
df_left = df[[key]]
df_values = df[value].str.split(", |,",expand=True)#expand=True单独成列,修改你的分隔符号
df_stack = df_values.stack()#行转列
df_stack = df_stack.reset_index(level=1,drop=True)#删除第二列索引
df_right = pd.concat([df_stack],axis=1,keys=(value,))#给第二列增加列名
df = pd.merge(df_left,df_right,how='left',left_index=True,right_index=True)#根据索引值进行left join
df = df.reset_index(level=0,drop=True)#重置索引
return df
# 这个版本是返回原df所有列
def row_to_col(df,value): #,value 需要转列的字段
key = 'index'
df['index'] = df.index
df_raw = df[[i for i in df.columns if i not in [key,value]]]
df_raw['index'] = df_raw.index
df_left = df[[key]]
df_values = df[value].str.split(",|,",expand=True)#expand=True单独成列,修改你的分隔符号
df_stack = df_values.stack()#行转列
df_stack = df_stack.reset_index(level=1,drop=True)#删除第二列索引
df_right = pd.concat([df_stack],axis=1,keys=(value,))#给第二列增加列名
df = pd.merge(df_left,df_right,how='left',left_index=True,right_index=True)#根据索引值进行left join
df = df.reset_index(level=0,drop=True)#重置索引
df = pd.merge(df,df_raw,on='index')#根据索引值进行left join
return df