呼唤远方
尝试组合Series.str.split, Series.stack, Series.rename, pandas.concat,DataFrame.assign和DataFrame.reset_index这样的:例子df = pd.DataFrame([{'col1': 'a;b;c', 'col2': 'w;x', 'col3': 1}, {'col1': 'd;e;f', 'col2': 'x;y', 'col3': 2}, {'col1': 'g;h;i', 'col2': 'z;u;v', 'col3': 3}, {'col1': '1,2,3', 'col2': '2', 'col3': 4}])print(df)# col1 col2 col3# 0 a;b;c w;x 1# 1 d;e;f x;y 2# 2 g;h;i z;u;v 3# 3 1,2,3 2 4df_new = (pd.concat([df[x].str.split('[;,]', expand=True).stack().rename(x) for x in df[['col1', 'col2']]], axis=1) .reset_index(level=1, drop=True) .assign(col3=df.col3))print(df_new) col1 col2 col30 a w 10 b x 10 c NaN 11 d x 21 e y 21 f NaN 22 g z 32 h u 32 i v 33 1 2 43 2 NaN 43 3 NaN 4