pandas provides methods for manipulating a Series and DataFrame to alter the
representation of the data for further data processing or data summarization.
pivot() and pivot_table(): Group unique values within one or more discrete categories.
and : Pivot a column or row level to the opposite axis respectively.
Data is often stored in so-called “stacked” or “record” format. In a “record” or “wide” format,
typically there is one row for each subject. In the “stacked” or “long” format there are
multiple rows for each subject where applicable.
In [1]: data={ ...: "value":range(12), ...: "variable":["A"]*3+["B"]*3+["C"]*3+["D"]*3, ...: "date":pd.to_datetime(["2020-01-03","2020-01-04","2020-01-05"]*4) ...: } ...: In [2]: df=pd.DataFrame(data)
To perform time series operations with each unique variable, a better
representation would be where the columns are the unique variables and an
index of dates identifies individual observations. To reshape the data into
this form, we use the DataFrame.pivot() method (also implemented as a
top level function pivot()):
In [3]: pivoted=df.pivot(index="date",columns="variable",values="value")In [4]: pivotedOut[4]: variable A B C Ddate 2020-01-03 0 3 6 92020-01-04 1 4 7 102020-01-05 2 5 8 11
If the values argument is omitted, and the input DataFrame has more than
one column of values which are not used as column or index inputs to pivot(),
then the resulting “pivoted” DataFrame will have hierarchical columns whose topmost level indicates the respective value
column:
In [5]: df["value2"]=df["value"]*2In [6]: pivoted=df.pivot(index="date",columns="variable")In [7]: pivotedOut[7]: value value2 variable A B C D A B C Ddate 2020-01-03 0 3 6 9 0 6 12 18
You can then select subsets from the pivoted DataFrame:
In [8]: pivoted["value2"]Out[8]: variable A B C Ddate 2020-01-03 0 6 12 182020-01-04 2 8 14 202020-01-05 4 10 16 22
Note that this returns a view on the underlying data in the case where the data
are homogeneously-typed.
While pivot() provides general purpose pivoting with various
data types, pandas also provides pivot_table() or pivot_table()
for pivoting with aggregation of numeric data.
The function pivot_table() can be used to create spreadsheet-style
pivot tables. See the for some advanced
strategies.
stack(): “pivot” a level of the (possibly hierarchical) column labels,
returning a DataFrame with an index with a new inner-most level of row
labels.
unstack(): (inverse operation of stack()) “pivot” a level of the
(possibly hierarchical) row index to the column axis, producing a reshaped
DataFrame with a new inner-most level of column labels.
In [20]: tuples=[ ....: ["bar","bar","baz","baz","foo","foo","qux","qux"], ....: ["one","two","one","two","one","two","one"
The stack() function “compresses” a level in the DataFrame columns to
produce either:
If the columns have a MultiIndex, you can choose which level to stack. The
stacked level becomes the new lowest level in a MultiIndex on the columns:
In [25]: stacked=df2.stack(future_stack=True)In [26]: stackedOut[26]: first second bar one A 0.895717 B 0.805244 two A -1.206412 B 2.565646baz one A 1.431256 B 1.340309 two A -1.170299 B -0.226169dtype: float64
In [27]: stacked.unstack()Out[27]: A Bfirst second bar one 0.895717 0.805244 two -1.206412 2.565646baz one 1.431256 1.340309 two -1.170299 -0.226169In [28]: stacked.unstack(1)Out[28]: second one twofirst bar A 0.895717 -1.206412 B 0.805244 2.565646baz A 1.431256 -1.170299 B 1.340309 -0.226169In [29]: stacked.unstack(0
If the indexes have names, you can use the level names instead of specifying
the level numbers:
In [30]: stacked.unstack("second")Out[30]: second one twofirst bar A 0.895717 -1.206412 B 0.805244 2.565646baz A 1.431256 -1.170299 B 1.340309 -0.226169
Notice that the stack() and unstack() methods implicitly sort the index
levels involved. Hence a call to stack() and then unstack(), or vice versa,
will result in a sorted copy of the original DataFrame or Series:
In [31]: index=pd.MultiIndex.from_product([[2,1],["a","b"]])In [32]: df=pd.DataFrame(np.random.randn(4),index=index,columns
Multiple levels
You may also stack or unstack more than one level at a time by passing a list
of levels, in which case the end result is as if each level in the list were
processed individually.
In [35]: columns=pd.MultiIndex.from_tuples( ....: [ ....: ("A","cat","long"), ....: ("B","cat","long"), ....:
Missing data
Unstacking can result in missing values if subgroups do not have the same
set of labels. By default, missing values will be replaced with the default
fill value for that data type.
The top-level melt() function and the corresponding DataFrame.melt()
are useful to massage a DataFrame into a format where one or more columns
are identifier variables, while all other columns, considered measured
variables, are “unpivoted” to the row axis, leaving just two non-identifier
columns, “variable” and “value”. The names of those columns can be customized
by supplying the var_name and value_name parameters.
When transforming a DataFrame using melt(), the index will be ignored.
The original index values can be kept by setting the ignore_index=False parameter to False (default is True).
ignore_index=False will however duplicate index values.
In [51]: index=pd.MultiIndex.from_tuples([("person","A"),("person","B")])In [52]: cheese=pd.DataFrame( ....: { ....: "first":["John","Mary"], ....:
wide_to_long() is similar to melt() with more customization for
column matching.
In [56]: dft=pd.DataFrame( ....: { ....: "A1970":{0:"a",1:"b",2:"c"}, ....: "A1980":{0:"d",1:"e",
To convert categorical variables of a Series into a “dummy” or “indicator”,
get_dummies() creates a new DataFrame with columns of the unique
variables and the values representing the presence of those variables per row.
In [60]: df=pd.DataFrame({"key":list("bbacab"),"data1":range(6)})In [61]: pd.get_dummies(df["key"])Out[61]: a b c0 False True False1 False True False2 True False False3 False False True4 True False False5 False True FalseIn [62]: df["key"]
prefix adds a prefix to the the column names which is useful for merging the result
with the original DataFrame:
get_dummies() also accepts a DataFrame. By default, object, string,
or categorical type columns are encoded as dummy variables with other columns unaltered.
In [70]: df=pd.DataFrame({"A":["a","b","a"],"B":["c","c","b"],"C":[1,2,3]})In [71]: pd.get_dummies
Specifying the columns keyword will encode a column of any type.
In [72]: pd.get_dummies(df,columns=["A"])Out[72]: B C A_a A_b0 c 1 True False1 c 2 False True2 b 3 True False
As with the Series version, you can pass values for the prefix and
prefix_sep. By default the column name is used as the prefix and _ as
the prefix separator. You can specify prefix and prefix_sep in 3 ways:
string: Use the same value for prefix or prefix_sep for each column
to be encoded.
list: Must be the same length as the number of columns being encoded.
Dummy coded data only requires k-1 categories to be included, in this case
the last category is the default category. The default category can be modified with
default_category.
For a DataFrame column with nested, list-like values, explode() will transform
each list-like value to a separate row. The resulting Index will be duplicated corresponding
to the index label from the original row:
Use crosstab() to compute a cross-tabulation of two (or more)
factors. By default crosstab() computes a frequency table of the factors
unless an array of values and an aggregation function are passed.
Any Series passed will have their name attributes used unless row or column
names for the cross-tabulation are specified
In [104]: a=np.array(["foo","foo","bar","bar","foo","foo"],dtype=object)In [105]: b=np.array(["one","one","two","one","two","one"],dtype=object)In [106]: c=np.array(["dull","dull","shiny","dull","dull","shiny"],dtype=object)In [107]: pd.crosstab(a,[b,c],rownames=["a"],colnames=["b","c"])Out[107]: b one two c dull shiny dull shinya bar 1 0 0 1foo 2 1 1 0
If crosstab() receives only two Series, it will provide a frequency table.
In [108]: df=pd.DataFrame( .....: {"A":[1,2,2,2,2],"B":[3,3,4,4,4],"C":[
In [111]: foo=pd.Categorical(["a","b"],categories=["a","b","c"])In [112]: bar=pd.Categorical(["d","e"],categories=["d","e",
For Categorical data, to include all of data categories even if the actual data does
not contain any instances of a particular category, use dropna=False.
In [114]: pd.crosstab(foo,bar,dropna=False)Out[114]: col_0 d e frow_0 a 1 0 0b 0 1 0c 0 0 0
Normalization
Frequency tables can also be normalized to show percentages rather than counts
using the normalize argument:
In [115]: pd.crosstab(df["A"],df["B"],normalize=True)Out[115]: B 3 4A 1 0.2 0.02 0.2 0.6
can also normalize values within each row or within each column:
Adding margins
margins=True will add a row and column with an All label with partial group aggregates
across the categories on the rows and columns:
In [118]: pd.crosstab( .....: df["A"],df["B"],values=df["C"],aggfunc="sum",normalize
The cut() function computes groupings for the values of the input
array and is often used to transform continuous variables to discrete or
categorical variables:
In [9]: importdatetimeIn [10]: df=pd.DataFrame( ....: { ....: "A":["one","one","two","three"]*6, ....: "B":["A","B","C"]*8, ....: "C":["foo","foo","foo","bar","bar","bar"]*4, ....: "D":np.random.randn(24), ....: "E":np.random.randn(24), ....: "F":[datetime.datetime(2013,i,1)foriinrange(1,13)] ....: +[datetime.datetime(2013,i,15)foriinrange(1,13)], ....: } ....: ) ....: In [11]: dfOut[11]: A B C D E F0 one A foo 0.469112 0.404705 2013-01-011 one B foo -0.282863 0.577046 2013-02-012 two C foo -1.509059 -1.715002 2013-03-013 three A bar -1.135632 -1.039268 2013-04-014 one B bar 1.212112 -0.370647 2013-05-01.. ... .. ... ... ... ...19 three B foo -1.087401 -0.472035 2013-08-1520 one C foo -0.673690 -0.013960 2013-09-1521 one A bar 0.113648 -0.362543 2013-10-1522 two B bar -1.478427 -0.006154 2013-11-1523 three C bar 0.524988 -0.923061 2013-12-15[24 rows x 6 columns]In [12]: pd.pivot_table(df,values="D",index=["A","B"],columns=["C"])Out[12]: C bar fooA B one A -0.995460 0.595334 B 0.393570 -0.494817 C 0.196903 -0.767769three A -0.431886 NaN B NaN -1.065818 C 0.798396 NaNtwo A NaN 0.197720 B -0.986678 NaN C NaN -1.274317In [13]: pd.pivot_table( ....: df,values=["D","E"], ....: index=["B"], ....: columns=["A","C"], ....: aggfunc="sum", ....: ) ....: Out[13]: D ... E A one three ... three two C bar foo bar ... foo bar fooB ... A -1.990921 1.190667 -0.863772 ... NaN NaN -1.067650B 0.787140 -0.989634 NaN ... 0.372851 1.63741 NaNC 0.393806 -1.535539 1.596791 ... NaN NaN -3.491906[3 rows x 12 columns]In [14]: pd.pivot_table( ....: df,values="E", ....: index=["B","C"], ....: columns=["A"], ....: aggfunc=["sum","mean"], ....: ) ....: Out[14]: sum mean A one three two one three twoB C A bar -0.471593 -2.008182 NaN -0.235796 -1.004091 NaN foo 0.761726 NaN -1.067650 0.380863 NaN -0.533825B bar -1.665170 NaN 1.637410 -0.832585 NaN 0.818705 foo -0.097554 0.372851 NaN -0.048777 0.186425 NaNC bar -0.744154 -2.392449 NaN -0.372077 -1.196224 NaN foo 1.061810 NaN -3.491906 0.530905 NaN -1.745953
The result is a DataFrame potentially having a MultiIndex on the
index or column. If the values column name is not given, the pivot table
will include all of the data in an additional level of hierarchy in the columns:
In [15]: pd.pivot_table(df[["A","B","C","D","E"]],index=["A","B"],columns=["C"])Out[15]: D E C bar foo bar fooA B one A -0.995460 0.595334 -0.235796 0.380863 B 0.393570 -0.494817 -0.832585 -0.048777 C 0.196903 -0.767769 -0.372077 0.530905three A -0.431886 NaN -1.004091 NaN B NaN -1.065818 NaN 0.186425 C 0.798396 NaN -1.196224 NaNtwo A NaN 0.197720 NaN -0.533825 B -0.986678 NaN 0.818705 NaN C NaN -1.274317 NaN -1.745953
In [16]: pd.pivot_table(df,values="D",index=pd.Grouper(freq="ME",key="F"),columns="C")Out[16]: C bar fooF 2013-01-31 NaN 0.5953342013-02-28 NaN -0.4948172013-03-31 NaN -1.2743172013-04-30 -0.431886 NaN2013-05-31 0.393570 NaN2013-06-30 0.196903 NaN2013-07-31 NaN 0.1977202013-08-31 NaN -1.0658182013-09-30 NaN -0.7677692013-10-31 -0.995460 NaN2013-11-30 -0.986678 NaN2013-12-31 0.798396 NaN
Adding margins
Passing margins=True to pivot_table() will add a row and column with an
All label with partial group aggregates across the categories on the
rows and columns:
In [17]: table=df.pivot_table( ....: index=["A","B"], ....: columns="C", ....: values=["D","E"], ....: margins=True, ....: aggfunc="std" ....: ) ....: In [18]: tableOut[18]: D E C bar foo All bar foo AllA B one A 1.568517 0.178504 1.293926 0.179247 0.033718 0.371275 B 1.157593 0.299748 0.860059 0.653280 0.885047 0.779837 C 0.523425 0.133049 0.638297 1.111310 0.770555 0.938819three A 0.995247 NaN 0.995247 0.049748 NaN 0.049748 B NaN 0.030522 0.030522 NaN 0.931203 0.931203 C 0.386657 NaN 0.386657 0.386312 NaN 0.386312two A NaN 0.111032 0.111032 NaN 1.146201 1.146201 B 0.695438 NaN 0.695438 1.166526 NaN 1.166526 C NaN 0.331975 0.331975 NaN 0.043771 0.043771All 1.014073 0.713941 0.871016 0.881376 0.984017 0.923568
Additionally, you can call DataFrame.stack() to display a pivoted DataFrame
as having a multi-level index:
In [19]: table.stack(future_stack=True)Out[19]: D EA B C one A bar 1.568517 0.179247 foo 0.178504 0.033718 All 1.293926 0.371275 B bar 1.157593 0.653280 foo 0.299748 0.885047... ... ...two C foo 0.331975 0.043771 All 0.331975 0.043771All bar 1.014073 0.881376 foo 0.713941 0.984017 All 0.871016 0.923568[30 rows x 2 columns]
,
"two"
],
....:
]
....:
In [21]:
index
=
pd
.
MultiIndex
.
from_arrays
(
tuples
,
names
=
[
"first"
,
"second"
])
In [22]:
df
=
pd
.
DataFrame
(
np
.
random
.
randn
(
8
,
2
),
index
=
index
,
columns
=
[
"A"
,
"B"
])
In [23]:
df2
=
df
[:
4
]
In [24]:
df2
Out[24]:
A B
first second
bar one 0.895717 0.805244
two -1.206412 2.565646
baz one 1.431256 1.340309
two -1.170299 -0.226169
)
Out[29]:
first bar baz
second
one A 0.895717 1.431256
B 0.805244 1.340309
two A -1.206412 -1.170299
B 2.565646 -0.226169
=
[
"A"
])
In [33]:
df
Out[33]:
A
2 a -1.413681
b 1.607920
1 a 1.024180
b 0.569605
In [34]:
all
(
df
.
unstack
()
.
stack
(
future_stack
=
True
)
==
df
.
sort_index
())
Out[34]:
True
(
"A"
,
"dog"
,
"short"
),
....:
(
"B"
,
"dog"
,
"short"
),
....:
],
....:
names
=
[
"exp"
,
"animal"
,
"hair_length"
],
....:
)
....:
In [36]:
df
=
pd
.
DataFrame
(
np
.
random
.
randn
(
4
,
4
),
columns
=
columns
)
In [37]:
df
Out[37]:
exp A B A B
animal cat cat dog dog
hair_length long long short short
0 0.875906 -2.211372 0.974466 -2.006747
1 -0.410001 -0.078638 0.545952 -1.219217
2 -1.226825 0.769804 -1.281247 -0.727707
3 -0.121306 -0.097883 0.695775 0.341734
In [38]:
df
.
stack
(
level
=
[
"animal"
,
"hair_length"
],
future_stack
=
True
)
Out[38]:
exp A B
animal hair_length
0 cat long 0.875906 -2.211372
dog short 0.974466 -2.006747
1 cat long -0.410001 -0.078638
dog short 0.545952 -1.219217
2 cat long -1.226825 0.769804
dog short -1.281247 -0.727707
3 cat long -0.121306 -0.097883
dog short 0.695775 0.341734
The list of levels can contain either level names or level numbers but
not a mixture of the two.
# df.stack(level=['animal', 'hair_length'], future_stack=True)# from above is equivalent to:In [39]: df.stack(level=[1,2],future_stack=True)Out[39]: exp A B animal hair_length 0 cat long 0.875906 -2.211372 dog short 0.974466 -2.0067471 cat long -0.410001 -0.078638 dog short 0.545952 -1.2192172 cat long -1.226825 0.769804 dog short -1.281247 -0.7277073 cat long -0.121306 -0.097883 dog short 0.695775 0.341734
....:
(
"A"
,
"dog"
),
....:
],
....:
names
=
[
"exp"
,
"animal"
],
....:
)
....:
In [41]:
index
=
pd
.
MultiIndex
.
from_product
(
....:
[(
"bar"
,
"baz"
,
"foo"
,
"qux"
),
(
"one"
,
"two"
)],
names
=
[
"first"
,
"second"
]
....:
)
....:
In [42]:
df
=
pd
.
DataFrame
(
np
.
random
.
randn
(
8
,
4
),
index
=
index
,
columns
=
columns
)
In [43]:
df3
=
df
.
iloc
[[
0
,
1
,
4
,
7
],
[
1
,
2
]]
In [44]:
df3
Out[44]:
exp B
animal dog cat
first second
bar one -1.110336 -0.619976
two 0.687738 0.176444
foo one 1.314232 0.690579
qux two 0.380396 0.084844
In [45]:
df3
.
unstack
()
Out[45]:
exp B
animal dog cat
second one two one two
first
bar -1.110336 0.687738 -0.619976 0.176444
foo 1.314232 NaN 0.690579 NaN
qux NaN 0.380396 NaN 0.084844
The missing value can be filled with a specific value with the fill_value argument.
In [46]: df3.unstack(fill_value=-1e9)Out[46]: exp B animal dog cat second one two one twofirst bar -1.110336e+00 6.877384e-01 -6.199759e-01 1.764443e-01foo 1.314232e+00 -1.000000e+09 6.905793e-01 -1.000000e+09qux -1.000000e+09 3.803956e-01 -1.000000e+09 8.484421e-02
In [116]: pd.crosstab(df["A"],df["B"],normalize="columns")Out[116]: B 3 4A 1 0.5 0.02 0.5 1.0
crosstab() can also accept a third Series and an aggregation function
(aggfunc) that will be applied to the values of the third Series within
each group defined by the first two Series:
In [117]: pd.crosstab(df["A"],df["B"],values=df["C"],aggfunc="sum")Out[117]: B 3 4A 1 1.0 NaN2 1.0 2.0