Melting back to long in Python
melt is the inverse of a pivot: wide columns become key-value rows.
import pandas as pd
wide = pd.DataFrame({
"day": ["mon", "tue"],
"python": [104, 91],
"rust": [98, 88],
})
long = wide.melt(id_vars="day", var_name="lang", value_name="tpm")
print(long)
print(long.groupby("lang")["tpm"].mean())
How it works
id_varsstays put; everything else is unstacked.var_nameandvalue_namename the two new columns.- Long data is what plotting and groupby want.
Keywords and builtins used here
asprint
The run, in numbers
- Lines
- 11
- Characters to type
- 229
- Tokens
- 95
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 52 seconds.
Step 4 of 4 in Reshaping & joining, step 22 of 26 in pandas.