-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API: add top-level melt function as method #15521
Changes from all commits
d54dc2f
3cbbed5
11f3fe4
ff895fe
5f306a9
28a38f2
1657246
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,7 +105,9 @@ | |
optional_by=""" | ||
by : str or list of str | ||
Name or list of names which refer to the axis items.""", | ||
versionadded_to_excel='') | ||
versionadded_to_excel='', | ||
versionadded_melt='\n.. versionadded:: 0.20.0\n', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this needs extra spaces (you can check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what you mean by this, can you clarify? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you do
so the versionadded is not indented as the other lines. But I think Jeff fixed it before merging |
||
other_melt='melt') | ||
|
||
_numeric_only_doc = """numeric_only : boolean, default None | ||
Include only float, int, boolean data. If None, will attempt to use | ||
|
@@ -4051,6 +4053,107 @@ def unstack(self, level=-1, fill_value=None): | |
from pandas.core.reshape import unstack | ||
return unstack(self, level, fill_value) | ||
|
||
_shared_docs['melt'] = """ | ||
"Unpivots" a DataFrame from wide format to long format, optionally leaving | ||
identifier variables set. | ||
|
||
This function is useful to massage a DataFrame into a format where one | ||
or more columns are identifier variables (`id_vars`), while all other | ||
columns, considered measured variables (`value_vars`), are "unpivoted" to | ||
the row axis, leaving just two non-identifier columns, 'variable' and | ||
'value'. | ||
%(versionadded_melt)s | ||
|
||
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 : scalar | ||
Name 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. | ||
|
||
See also | ||
-------- | ||
%(other_melt)s | ||
pivot_table | ||
DataFrame.pivot | ||
|
||
Examples | ||
-------- | ||
>>> import pandas as pd | ||
>>> 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 | ||
A B C | ||
0 a 1 2 | ||
1 b 3 4 | ||
2 c 5 6 | ||
|
||
>>> pd.melt(df, id_vars=['A'], value_vars=['B']) | ||
A variable value | ||
0 a B 1 | ||
1 b B 3 | ||
2 c B 5 | ||
|
||
>>> 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 | ||
|
||
The names of 'variable' and 'value' columns can be customized: | ||
|
||
>>> pd.melt(df, id_vars=['A'], value_vars=['B'], | ||
... var_name='myVarname', value_name='myValname') | ||
A myVarname myValname | ||
0 a B 1 | ||
1 b B 3 | ||
2 c B 5 | ||
|
||
If you have multi-index columns: | ||
|
||
>>> df.columns = [list('ABC'), list('DEF')] | ||
>>> df | ||
A B C | ||
D E F | ||
0 a 1 2 | ||
1 b 3 4 | ||
2 c 5 6 | ||
|
||
>>> pd.melt(df, col_level=0, id_vars=['A'], value_vars=['B']) | ||
A variable value | ||
0 a B 1 | ||
1 b B 3 | ||
2 c B 5 | ||
|
||
>>> pd.melt(df, id_vars=[('A', 'D')], value_vars=[('B', 'E')]) | ||
(A, D) variable_0 variable_1 value | ||
0 a B E 1 | ||
1 b B E 3 | ||
2 c B E 5 | ||
|
||
""" | ||
|
||
@Appender(_shared_docs['melt'] % _shared_doc_kwargs) | ||
def melt(self, id_vars=None, value_vars=None, var_name=None, | ||
value_name='value', col_level=None): | ||
from pandas.core.reshape import melt | ||
return melt(self, id_vars=id_vars, value_vars=value_vars, | ||
var_name=var_name, value_name=value_name, | ||
col_level=col_level) | ||
|
||
# ---------------------------------------------------------------------- | ||
# Time series-related | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you mention here both?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done (GitHub is not folding this review component for some reason, however).