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
pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])
| A | variable | value |
---|
0 | a | B | 1 |
---|
1 | b | B | 3 |
---|
2 | c | B | 5 |
---|
3 | a | C | 2 |
---|
4 | b | C | 4 |
---|
5 | c | C | 6 |
---|
seaborn iris数据集
df= sns.load_dataset('iris')
df.head()
| sepal_length | sepal_width | petal_length | petal_width | species |
---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
---|
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
---|
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
---|
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
---|
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
---|
df=pd.melt(df,'species', var_name="measurement")
df.head()
| species | measurement | value |
---|
0 | setosa | sepal_length | 5.1 |
---|
1 | setosa | sepal_length | 4.9 |
---|
2 | setosa | sepal_length | 4.7 |
---|
3 | setosa | sepal_length | 4.6 |
---|
4 | setosa | sepal_length | 5.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)
0 Comments