2 回答

TA贡献1836条经验 获得超13个赞
替代代码:
下面的代码使用.stack()
,.unstack()
和.pivot_table()
。它有选择地只选择在要拆分的列表中指定字符串small
, large
,的列名。con
key
注意:
输出
aggfunc=np.max
在 中聚合.pivot_table()
。这可以根据需要更改为任何其他类型的聚合方法,例如np.min
,np.sum
或自定义函数。原来的列名改为小写,除了
Time
代码:
# Import libraries
import 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 index
df = df.set_index('Time')
# Rename columns
df.columns = df.columns.str.lower() # change case to lower
# Stack
df = df.stack().reset_index() # convert columns to rows
# Split based on condition
key = ['small', 'large','con'] # Column names to be split
df['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 columns
df = df.drop(['level_1'], axis=1)
df.columns.name=''
# Pivot using aggregate function: np.max
df = df.pivot_table(index=['Time', 'New_Column'], columns='col1', values=0, aggfunc=np.max)
# Rearrange
df = df.reset_index()
df.columns.name=''
df = df[['Time','small', 'large', 'con', 'New_Column']]
输出
print(df)
Time small large con New_Column
0 12/1/19 0:00 1 0 0 1
1 12/1/19 0:00 1 0 0 10
2 12/1/19 0:00 0 0 0 2
3 12/1/19 0:05 0 0 0 1
4 12/1/19 0:05 0 0 0 10
5 12/1/19 0:05 0 0 0 2

TA贡献1804条经验 获得超2个赞
我首先建议filter感兴趣的列,然后是melt列,然后是split“_”上的列名称,然后是pivot数据框:
# Filter columns
df = df.filter(regex=r"^([Cc]on|[Ll]arge|[Ss]mall|Time).*")
# Melt dataframe
new = df.melt(id_vars="Time")
# Split column name
new[["variable", "New_Column"]] = new.variable.str.split("_", expand=True)
# Set variable as title
new["variable"] = new.variable.str.title()
# Pivot dataframe
new = pd.pivot_table(new, index=["Time", "New_Column"], values="value", columns="variable")
print(new.reset_index())
添加回答
举报