开心每一天1111
替代代码:下面的代码使用.stack(),.unstack()和.pivot_table()。它有选择地只选择在要拆分的列表中指定字符串small, large,的列名。conkey注意:输出aggfunc=np.max在 中聚合.pivot_table()。这可以根据需要更改为任何其他类型的聚合方法,例如np.min,np.sum或自定义函数。原来的列名改为小写,除了Time代码:# Import librariesimport pandas as pd# Create DataFrame (copy pasted from question above)data = {'Time': ['12/1/19 0:00', '12/1/19 0:05'], 'Small_1': [1, 0], 'Large_1': [0, 0], 'Con_1': [0, 0], 'Small_2': [0, 0], 'Large_2': [0, 0], 'Con_2': [0, 0], 'Small_10': [1, 0], 'large_10': [0, 0], 'Con_10': [0, 0], 'Some_other_value': [78, 96], }df = pd.DataFrame(data)# Set Time as indexdf = df.set_index('Time')# Rename columnsdf.columns = df.columns.str.lower() # change case to lower# Stackdf = df.stack().reset_index() # convert columns to rows# Split based on conditionkey = ['small', 'large','con'] # Column names to be splitdf['col1'] = df['level_1'].apply(lambda x: x.split('_')[0] if x.split('_')[0] in key else x)df['New_Column'] = df['level_1'].apply(lambda x: x.split('_')[1] if x.split('_')[0] in key else np.NaN)# Drop/rename columnsdf = df.drop(['level_1'], axis=1)df.columns.name=''# Pivot using aggregate function: np.maxdf = df.pivot_table(index=['Time', 'New_Column'], columns='col1', values=0, aggfunc=np.max)# Rearrangedf = df.reset_index()df.columns.name=''df = df[['Time','small', 'large', 'con', 'New_Column']]输出print(df) Time small large con New_Column0 12/1/19 0:00 1 0 0 11 12/1/19 0:00 1 0 0 102 12/1/19 0:00 0 0 0 23 12/1/19 0:05 0 0 0 14 12/1/19 0:05 0 0 0 105 12/1/19 0:05 0 0 0 2