pandas.melt

pandas.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name=’value’, col_level=None)

Parameters:

  • frame : DataFrame
  • id_vars : tuple, list, or ndarray, optional,Column(s) to use as identifier variables.
  • value_vars : tuple, list, or ndarray, optional,Column(s) to unpivot. If not specified, uses all columns that are not set as id_vars.
  • var_name : scalarName to use for the ‘variable’ column. If None it uses frame.columns.name or ‘variable’.
  • value_name : scalar, default ‘value’Name to use for the ‘value’ column.
  • col_level : int or string, optional, If columns are a MultiIndex then use this level to melt.

简单讲:将一个DataFrame根据id_vars列,把value_vars列的值平铺展开,不设置值时,默认为所有列。

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import warnings
import numpy as np
warnings.filterwarnings('ignore')
举例
df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
                    'B': {0: 1, 1: 3, 2: 5},
                    'C': {0: 2, 1: 4, 2: 6}})
df
ABC
0a12
1b34
2c56
pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])
Avariablevalue
0aB1
1bB3
2cB5
3aC2
4bC4
5cC6
seaborn iris数据集
df= sns.load_dataset('iris')
df.head()
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa
df=pd.melt(df,'species', var_name="measurement")
df.head()
speciesmeasurementvalue
0setosasepal_length5.1
1setosasepal_length4.9
2setosasepal_length4.7
3setosasepal_length4.6
4setosasepal_length5.0

sns.stripplot()

sns.set(style="whitegrid")
iris = sns.load_dataset("iris")

# "Melt" the dataset to "long-form" or "tidy" representation
iris = pd.melt(iris, "species", var_name="measurement")

# Initialize the figure
f, ax = plt.subplots()
sns.despine(bottom=True, left=True)

# Show each observation with a scatterplot
sns.stripplot(x="value", y="measurement", hue="species",
              data=iris, dodge=True, alpha=.25, zorder=1)

# Show the conditional means
sns.pointplot(x="value", y="measurement", hue="species",
              data=iris, dodge=.532, join=False, palette="dark",
              markers="d", scale=.75, ci=None)

# Improve the legend 
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[3:], labels[3:], title="species",
          handletextpad=0, columnspacing=1,
          loc="lower right", ncol=3, frameon=True)
Categories: Python

0 Comments

Leave a Reply

Your email address will not be published.