简体   繁体   English

使用 pd.melt 和 merge 为 Seaborn 和 matplotlib 创建 DataFrame 时如何保留索引

[英]How to keep the index when using pd.melt and merge to create a DataFrame for Seaborn and matplotlib

I am trying to draw subplots using two identical DataFrames ( predicted and observed) with exact same structure... the first column is index我正在尝试使用两个具有完全相同结构的相同数据帧(预测和观察)绘制子图...第一列是索引

The code below makes new index when they are concatenated using pd.melt and merge as you can see in the figure the index of orange line is changed from 1-5 to 6-10下面的代码在使用 pd.melt 和 merge 连接它们时创建新索引,如图所示,橙色线的索引从 1-5 更改为 6-10

I was wondering if some could fix the code below to keep the same index for the orange line:我想知道是否有人可以修复下面的代码以保持橙色线的相同索引:

import pandas as pd 
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

actual = pd.DataFrame({'a': [5, 8, 9, 6, 7, 2],
                       'b': [89, 22, 44, 6, 44, 1]})
predicted = pd.DataFrame({'a': [7, 2, 13, 18, 20, 2],
                       'b': [9, 20, 4, 16, 40, 11]})

# Creating a tidy-dataframe to input under seaborn
merged = pd.concat([pd.melt(actual), pd.melt(predicted)]).reset_index()
merged['category'] = ''
merged.loc[:len(actual)*2,'category'] = 'actual'
merged.loc[len(actual)*2:,'category'] = 'predicted'

g = sns.FacetGrid(merged, col="category", hue="variable")
g.map(plt.plot, "index", "value", alpha=.7)
g.add_legend();

在此处输入图像描述

The orange line ( 'variable' == 'b' ) doesn't have an index of 0-5 because of how you used melt .由于您使用melt的方式,橙色线 ( 'variable' == 'b' ) 没有0-5的索引。 If you look at pd.melt(actual) , the index doesn't match what you are expecting, IIUC.如果您查看pd.melt(actual) ,该索引与您的预期不符,IIUC。

Here is how I would rearrange the dataframe:以下是我将如何重新排列数据框:

merged = pd.concat([actual, predicted], keys=['actual', 'predicted'])
merged.index.names = ['category', 'index']
merged = merged.reset_index()
merged = pd.melt(merged, id_vars=['category', 'index'], value_vars=['a', 'b'])

Set the ignore_index variable to false to preserve the index., eg将 ignore_index 变量设置为 false 以保留索引。例如

df = df.melt(var_name='species', value_name='height', ignore_index = False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM