You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Get source code for this RMarkdown script [here](https://raw.githubusercontent.com/hauselin/rtutorialsite/master/_posts/2020-05-17-reshape-python-pandas-dataframe-from-long-to-wide-with-pivottable/reshape-python-pandas-dataframe-from-long-to-wide-with-pivottable.Rmd).
29
29
30
-
This Python tutorial is also available on [Medium, Towards Data Science](https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02). Click here if you're looking for the R version of melt (also the `melt` function in R).
30
+
This Python tutorial is on [Medium, Towards Data Science](https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02). Click here if you're looking for the tutorial for the R version of melt (also the `melt` function in R).
31
31
32
32
## Consider being a patron and supporting my work?
33
33
@@ -37,7 +37,7 @@ How does the pandas `melt()` method reshape dataframes? How do you reshape a dat
37
37
38
38

39
39
40
-
Also, you might want to check out the official pandas documentation and my [numpy reshape tutorial](https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02).
40
+
Also, you might want to check out the official pandas documentation and my [numpy reshape tutorial](https://towardsdatascience.com/reshaping-numpy-arrays-in-python-a-step-by-step-pictorial-tutorial-aed5f471cf0b?source=friends_link&sk=3dc229066e89cb5bcd4bb86b301f0a5c).
41
41
42
42
Let's load the `reticulate` library in R so we can run Python in RStudio. I'll then load my conda python environment. I highly recommend you try the code in Python while you read this article. Try running this tutorial on my shared [DeepNote notebook](https://beta.deepnote.com/project/d1350ad6-bcb4-4c3d-857b-c29d01b8a803) (you can only run but not edit this notebook).
43
43
@@ -83,7 +83,7 @@ df_wide
83
83
84
84
Common terms for this transformation are melt, unpivot, gather, stack. See `pd.melt()` documentation [here](https://pandas.pydata.org/docs/reference/api/pandas.melt.html).
85
85
86
-
### Melt Example 1
86
+
### Melt example 1
87
87
88
88
We melt the dataframe by specifying the identifier columns via `id_vars`. The “leftover” non-identifier columns (english, math, physics) will be melted or stacked onto each other into one column.
89
89
@@ -101,7 +101,7 @@ pd.melt(frame=df_wide,
101
101
102
102

103
103
104
-
### Melt Example 2
104
+
### Melt example 2
105
105
106
106
You can use `value_vars` to specify which columns you want to melt or stack into column (here, we exclude physics column, so `value_vars=["english", "math"]`). We also drop the school column from `id_vars`.
107
107
@@ -118,7 +118,7 @@ pd.melt(frame=df_wide,
118
118
119
119

120
120
121
-
### Melt Example 3
121
+
### Melt example 3
122
122
123
123
Finally, let’s see what happens if we specify only the student column as the identifier column (`id_vars="student"`) but do not specify which columns you want to stack via `value_vars`. As a result, all non-identifier columns (school, english, math, physics) will be stacked into one column.
124
124
@@ -140,6 +140,6 @@ This table looks wrong because the `school` column in `df_wide` doesn't belong
140
140
141
141
## Support my work
142
142
143
-
I hope now you have a better understanding of how `pd.melt` reshapes dataframes. If you find this post useful, check out my other articles and visit my [Medium page](https://medium.com/@ausius).
143
+
I hope now you have a better understanding of how `pd.melt` reshapes dataframes. If you find this post useful, check out my other articles and folow me on [Medium ](https://medium.com/@ausius).
144
144
145
145
If you've enjoyed my article, [support my work and become a patron here](https://donorbox.org/support-my-teaching)!
Copy file name to clipboardExpand all lines: _posts/2020-05-14-reshaping-data-in-python-pandas/reshaping-data-in-python-pandas.html
+9-9
Original file line number
Diff line number
Diff line change
@@ -969,23 +969,23 @@ <h3 class="d-toc-header">Table of Contents</h3>
969
969
<li><ahref="#consider-being-a-patron-and-supporting-my-work">Consider being a patron and supporting my work?</a></li>
970
970
<li><ahref="#wide-data">Wide data</a></li>
971
971
<li><ahref="#wide-to-long-with-melt">Wide to long with <code>melt</code></a><ul>
972
-
<li><ahref="#melt-example-1">Melt Example 1</a></li>
973
-
<li><ahref="#melt-example-2">Melt Example 2</a></li>
974
-
<li><ahref="#melt-example-3">Melt Example 3</a></li>
972
+
<li><ahref="#melt-example-1">Melt example 1</a></li>
973
+
<li><ahref="#melt-example-2">Melt example 2</a></li>
974
+
<li><ahref="#melt-example-3">Melt example 3</a></li>
975
975
</ul></li>
976
976
<li><ahref="#support-my-work">Support my work</a></li>
977
977
</ul>
978
978
</nav>
979
979
<hrclass="d-toc-separator"/>
980
980
<p>Get source code for this RMarkdown script <ahref="https://raw.githubusercontent.com/hauselin/rtutorialsite/master/_posts/2020-05-17-reshape-python-pandas-dataframe-from-long-to-wide-with-pivottable/reshape-python-pandas-dataframe-from-long-to-wide-with-pivottable.Rmd">here</a>.</p>
981
-
<p>This Python tutorial is also available on <ahref="https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02">Medium, Towards Data Science</a>. Click here if you’re looking for the R version of melt (also the <code>melt</code> function in R).</p>
981
+
<p>This Python tutorial is on <ahref="https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02">Medium, Towards Data Science</a>. Click here if you’re looking for the tutorial for the R version of melt (also the <code>melt</code> function in R).</p>
982
982
<h2id="consider-being-a-patron-and-supporting-my-work">Consider being a patron and supporting my work?</h2>
983
983
<p><ahref="https://donorbox.org/support-my-teaching">Donate and become a patron</a>: If you find value in what I do and have learned something from my site, please consider becoming a patron. It takes me many hours to research, learn, and put together tutorials. Your support really matters.</p>
984
984
<p>How does the pandas <code>melt()</code> method reshape dataframes? How do you reshape a dataframe from wide to long form? This tutorial will walk you through reshaping dataframes using <code>pd.melt()</code> or the <code>melt</code> method associated with pandas dataframes.</p>
985
985
<figure>
986
986
<imgsrc="main_wide2long.png" alt="Summary of how pd.melt() works" /><figcaption>Summary of how pd.melt() works</figcaption>
987
987
</figure>
988
-
<p>Also, you might want to check out the official pandas documentation and my <ahref="https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02">numpy reshape tutorial</a>.</p>
988
+
<p>Also, you might want to check out the official pandas documentation and my <ahref="https://towardsdatascience.com/reshaping-numpy-arrays-in-python-a-step-by-step-pictorial-tutorial-aed5f471cf0b?source=friends_link&sk=3dc229066e89cb5bcd4bb86b301f0a5c">numpy reshape tutorial</a>.</p>
989
989
<p>Let’s load the <code>reticulate</code> library in R so we can run Python in RStudio. I’ll then load my conda python environment. I highly recommend you try the code in Python while you read this article. Try running this tutorial on my shared <ahref="https://beta.deepnote.com/project/d1350ad6-bcb4-4c3d-857b-c29d01b8a803">DeepNote notebook</a> (you can only run but not edit this notebook).</p>
<h2id="wide-to-long-with-melt">Wide to long with <code>melt</code></h2>
1033
1033
<p>Common terms for this transformation are melt, unpivot, gather, stack. See <code>pd.melt()</code> documentation <ahref="https://pandas.pydata.org/docs/reference/api/pandas.melt.html">here</a>.</p>
1034
-
<h3id="melt-example-1">Melt Example 1</h3>
1034
+
<h3id="melt-example-1">Melt example 1</h3>
1035
1035
<p>We melt the dataframe by specifying the identifier columns via <code>id_vars</code>. The “leftover” non-identifier columns (english, math, physics) will be melted or stacked onto each other into one column.</p>
1036
1036
<p>A new indicator column will be created (contains values english, math, physics) and we can rename this new column (cLaSs) via <code>var_name</code>. We can also rename the column in which all the actual grades are contained (gRaDe) via <code>value_name</code>.</p>
1037
1037
<divclass="layout-chunk" data-layout="l-body">
@@ -1061,7 +1061,7 @@ <h3 id="melt-example-1">Melt Example 1</h3>
1061
1061
<figure>
1062
1062
<imgsrc="pic3.png" alt="Wide to long: new indicator column “cLaSs” + values melted/stacked “gRaDe” column" /><figcaption>Wide to long: new indicator column “cLaSs” + values melted/stacked “gRaDe” column</figcaption>
1063
1063
</figure>
1064
-
<h3id="melt-example-2">Melt Example 2</h3>
1064
+
<h3id="melt-example-2">Melt example 2</h3>
1065
1065
<p>You can use <code>value_vars</code> to specify which columns you want to melt or stack into column (here, we exclude physics column, so <code>value_vars=["english", "math"]</code>). We also drop the school column from <code>id_vars</code>.</p>
1066
1066
<divclass="layout-chunk" data-layout="l-body">
1067
1067
<preclass="python"><code>
@@ -1087,7 +1087,7 @@ <h3 id="melt-example-2">Melt Example 2</h3>
1087
1087
<figure>
1088
1088
<imgsrc="pic4.png" alt="Wide to long: original columns school and physics have been dropped" /><figcaption>Wide to long: original columns school and physics have been dropped</figcaption>
1089
1089
</figure>
1090
-
<h3id="melt-example-3">Melt Example 3</h3>
1090
+
<h3id="melt-example-3">Melt example 3</h3>
1091
1091
<p>Finally, let’s see what happens if we specify only the student column as the identifier column (<code>id_vars="student"</code>) but do not specify which columns you want to stack via <code>value_vars</code>. As a result, all non-identifier columns (school, english, math, physics) will be stacked into one column.</p>
1092
1092
<p>The resulting long dataframe looks wrong because now the cLaSs and gRaDe columns contain values that shouldn’t be there. The point here is to show you how <code>pd.melt</code> works.</p>
1093
1093
<divclass="layout-chunk" data-layout="l-body">
@@ -1123,7 +1123,7 @@ <h3 id="melt-example-3">Melt Example 3</h3>
1123
1123
<imgsrc="pic5.png" alt="Wide to long: school column isn’t an identifier column" /><figcaption>Wide to long: school column isn’t an identifier column</figcaption>
1124
1124
</figure>
1125
1125
<h2id="support-my-work">Support my work</h2>
1126
-
<p>I hope now you have a better understanding of how <code>pd.melt</code> reshapes dataframes. If you find this post useful, check out my other articles and visit my <ahref="https://medium.com/@ausius">Medium page</a>.</p>
1126
+
<p>I hope now you have a better understanding of how <code>pd.melt</code> reshapes dataframes. If you find this post useful, check out my other articles and folow me on <ahref="https://medium.com/@ausius">Medium</a>.</p>
1127
1127
<p>If you’ve enjoyed my article, <ahref="https://donorbox.org/support-my-teaching">support my work and become a patron here</a>!</p>
Copy file name to clipboardExpand all lines: _posts/2020-05-17-reshape-python-pandas-dataframe-from-long-to-wide-with-pivottable/reshape-python-pandas-dataframe-from-long-to-wide-with-pivottable.Rmd
Get source code for this RMarkdown script [here](https://raw.githubusercontent.com/hauselin/rtutorialsite/master/_posts/2020-05-14-reshaping-data-in-python-pandas/reshaping-data-in-python-pandas.Rmd).
28
+
Get source code for this RMarkdown script [here](https://raw.githubusercontent.com/hauselin/rtutorialsite/master/_posts/2020-05-17-reshape-python-pandas-dataframe-from-long-to-wide-with-pivottable/reshape-python-pandas-dataframe-from-long-to-wide-with-pivottable.Rmd).
29
29
30
-
This Python tutorial is also available on [Medium, Towards Data Science](https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02). Click here if you're looking for the R version of melt (also the `dcast` function in R).
30
+
This Python tutorial is also on [Medium, Towards Data Science](https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02). Click here if you're looking for the tutorail for the R version of `pivot_table` (also the `dcast` function in R).
31
+
32
+
The opposite of `pivot_table` is `melt`, and you can find the tutorial for `melt` (wide to long) [here](https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02?source=friends_link&sk=5200888fd2030a2ede726c00dec8b610).
31
33
32
34
## Consider being a patron and supporting my work?
33
35
34
36
[Donate and become a patron](https://donorbox.org/support-my-teaching): If you find value in what I do and have learned something from my site, please consider becoming a patron. It takes me many hours to research, learn, and put together tutorials. Your support really matters.
35
37
36
-
How does the pandas `pivot_table()` method reshape dataframes? How do you reshape a dataframe from wide to long form? This tutorial will walk you through reshaping dataframes using `pd.pivot_table()` or the `pivot_table` method associated with pandas dataframes.
38
+
How does the pandas `pivot_table()` method reshape dataframes? How do you reshape a dataframe from long to wide form? This tutorial will walk you through reshaping dataframes using `pd.pivot_table()` or the `pivot_table` method associated with pandas dataframes.
37
39
38
40

39
41
40
-
Also, you might want to check out the official pandas documentation and my [numpy reshape tutorial](https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02).
42
+
Also, you might want to check out the official pandas documentation and my [numpy reshape tutorial](https://towardsdatascience.com/reshaping-numpy-arrays-in-python-a-step-by-step-pictorial-tutorial-aed5f471cf0b?source=friends_link&sk=3dc229066e89cb5bcd4bb86b301f0a5c).
41
43
42
44
Let's load the `reticulate` library in R so we can run Python in RStudio. I'll then load my conda python environment. I highly recommend you try the code in Python while you read this article. Try running this tutorial on my shared [DeepNote notebook](https://beta.deepnote.com/project/b494e5b7-9e5c-4e06-8a4a-43130d27aa3f) (you can only run but not edit this notebook).
I hope now you have a better understanding of how `pd.pivot_table` reshapes dataframes. If you find this post useful, check out my other articles and visit my [Medium page](https://medium.com/@ausius).
150
+
I hope now you have a better understanding of how `pd.pivot_table` reshapes dataframes. If you find this post useful, check out my other articles and folow me on [Medium ](https://medium.com/@ausius).
149
151
150
152
If you've enjoyed my article, [support my work and become a patron here](https://donorbox.org/support-my-teaching)!
Copy file name to clipboardExpand all lines: _posts/2020-05-17-reshape-python-pandas-dataframe-from-long-to-wide-with-pivottable/reshape-python-pandas-dataframe-from-long-to-wide-with-pivottable.html
+6-5
Original file line number
Diff line number
Diff line change
@@ -976,15 +976,16 @@ <h3 class="d-toc-header">Table of Contents</h3>
976
976
</ul>
977
977
</nav>
978
978
<hrclass="d-toc-separator"/>
979
-
<p>Get source code for this RMarkdown script <ahref="https://raw.githubusercontent.com/hauselin/rtutorialsite/master/_posts/2020-05-14-reshaping-data-in-python-pandas/reshaping-data-in-python-pandas.Rmd">here</a>.</p>
980
-
<p>This Python tutorial is also available on <ahref="https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02">Medium, Towards Data Science</a>. Click here if you’re looking for the R version of melt (also the <code>dcast</code> function in R).</p>
979
+
<p>Get source code for this RMarkdown script <ahref="https://raw.githubusercontent.com/hauselin/rtutorialsite/master/_posts/2020-05-17-reshape-python-pandas-dataframe-from-long-to-wide-with-pivottable/reshape-python-pandas-dataframe-from-long-to-wide-with-pivottable.Rmd">here</a>.</p>
980
+
<p>This Python tutorial is also on <ahref="https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02">Medium, Towards Data Science</a>. Click here if you’re looking for the tutorail for the R version of <code>pivot_table</code> (also the <code>dcast</code> function in R).</p>
981
+
<p>The opposite of <code>pivot_table</code> is <code>melt</code>, and you can find the tutorial for <code>melt</code> (wide to long) <ahref="https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02?source=friends_link&sk=5200888fd2030a2ede726c00dec8b610">here</a>.</p>
981
982
<h2id="consider-being-a-patron-and-supporting-my-work">Consider being a patron and supporting my work?</h2>
982
983
<p><ahref="https://donorbox.org/support-my-teaching">Donate and become a patron</a>: If you find value in what I do and have learned something from my site, please consider becoming a patron. It takes me many hours to research, learn, and put together tutorials. Your support really matters.</p>
983
-
<p>How does the pandas <code>pivot_table()</code> method reshape dataframes? How do you reshape a dataframe from wide to long form? This tutorial will walk you through reshaping dataframes using <code>pd.pivot_table()</code> or the <code>pivot_table</code> method associated with pandas dataframes.</p>
984
+
<p>How does the pandas <code>pivot_table()</code> method reshape dataframes? How do you reshape a dataframe from long to wide form? This tutorial will walk you through reshaping dataframes using <code>pd.pivot_table()</code> or the <code>pivot_table</code> method associated with pandas dataframes.</p>
984
985
<figure>
985
986
<imgsrc="pic1.png" alt="Summary of how pd.pivot_table() works" /><figcaption>Summary of how pd.pivot_table() works</figcaption>
986
987
</figure>
987
-
<p>Also, you might want to check out the official pandas documentation and my <ahref="https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02">numpy reshape tutorial</a>.</p>
988
+
<p>Also, you might want to check out the official pandas documentation and my <ahref="https://towardsdatascience.com/reshaping-numpy-arrays-in-python-a-step-by-step-pictorial-tutorial-aed5f471cf0b?source=friends_link&sk=3dc229066e89cb5bcd4bb86b301f0a5c">numpy reshape tutorial</a>.</p>
988
989
<p>Let’s load the <code>reticulate</code> library in R so we can run Python in RStudio. I’ll then load my conda python environment. I highly recommend you try the code in Python while you read this article. Try running this tutorial on my shared <ahref="https://beta.deepnote.com/project/b494e5b7-9e5c-4e06-8a4a-43130d27aa3f">DeepNote notebook</a> (you can only run but not edit this notebook).</p>
<p>I hope now you have a better understanding of how <code>pd.pivot_table</code> reshapes dataframes. If you find this post useful, check out my other articles and visit my <ahref="https://medium.com/@ausius">Medium page</a>.</p>
1158
+
<p>I hope now you have a better understanding of how <code>pd.pivot_table</code> reshapes dataframes. If you find this post useful, check out my other articles and folow me on <ahref="https://medium.com/@ausius">Medium</a>.</p>
1158
1159
<p>If you’ve enjoyed my article, <ahref="https://donorbox.org/support-my-teaching">support my work and become a patron here</a>!</p>
0 commit comments