使用 Pandas 展平数据

如何更改包含 Small_X、Large_X(其中 X 是数字 1、2、3 等)的列需要与传播到新记录的所有其他值一起展开,一个名为“New_Column”的新列将是添加表示 X 值。


import pandas as pd

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)

http://img.mukewang.com/63ec9dcc0001f2d108750112.jpg

以上输入数据应使用 Pandas 生成以下格式。

http://img.mukewang.com/63ec9dd700010fdf04620196.jpg


喵喔喔
浏览 288回答 2
2回答

开心每一天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

慕婉清6462132

我首先建议filter感兴趣的列,然后是melt列,然后是split“_”上的列名称,然后是pivot数据框:# Filter columnsdf = df.filter(regex=r"^([Cc]on|[Ll]arge|[Ss]mall|Time).*") # Melt dataframenew = df.melt(id_vars="Time")# Split column namenew[["variable", "New_Column"]] = new.variable.str.split("_", expand=True)# Set variable as titlenew["variable"] = new.variable.str.title()# Pivot dataframenew = pd.pivot_table(new, index=["Time", "New_Column"], values="value", columns="variable")print(new.reset_index())        
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python