| [ Web Proxy ] |
| Viewing: https://dataframes.bigquery.dev/reference/api/bigframes.pandas.DataFrame.melt.html | [Back] [Original] |
Section Navigation
Unpivot a DataFrame from wide to long format, optionally leaving identifiers 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.
Examples:
>>> df = bpd.DataFrame({"A": [1, None, 3, 4, 5], ... "B": [1, 2, 3, 4, 5], ... "C": [None, 3.5, None, 4.5, 5.0]}) >>> df A B C 0 1.0 1 <NA> 1 <NA> 2 3.5 2 3.0 3 <NA> 3 4.0 4 4.5 4 5.0 5 5.0 [5 rows x 3 columns]
Using melt without optional arguments:
>>> df.melt()
variable value
0 A 1.0
1 A <NA>
2 A 3.0
3 A 4.0
4 A 5.0
5 B 1.0
6 B 2.0
7 B 3.0
8 B 4.0
9 B 5.0
...
[15 rows x 2 columns]
Using melt with id_vars and value_vars:
>>> df.melt(id_vars='A', value_vars=['B', 'C'])
A variable value
0 1.0 B 1.0
1 <NA> B 2.0
2 3.0 B 3.0
3 4.0 B 4.0
4 5.0 B 5.0
5 1.0 C <NA>
6 <NA> C 3.5
7 3.0 C <NA>
8 4.0 C 4.5
9 5.0 C 5.0
[10 rows x 3 columns]
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.
Unpivoted DataFrame.
Copyright 2019, Google.
Created using Sphinx 8.1.3.
Built with the PyData Sphinx Theme 0.19.0.
| Web Proxy Viewer | New URL | Original Page |