[1]:
import pandas as pd
pd.set_option("display.max_rows", 20)
SpreadΒΆ
[2]:
from siuba import _, spread, gather
from siuba.data import mtcars
[3]:
costs = pd.DataFrame({
'id': [1,2],
'price_x': [.1, .2],
'price_y': [.4, .5],
'price_z': [.7, .8]
})
costs
[3]:
| id | price_x | price_y | price_z | |
|---|---|---|---|---|
| 0 | 1 | 0.1 | 0.4 | 0.7 |
| 1 | 2 | 0.2 | 0.5 | 0.8 |
[4]:
long = costs >> gather('measure', 'value', _.price_x, _.price_y, _.price_z)
long
[4]:
| id | measure | value | |
|---|---|---|---|
| 0 | 1 | price_x | 0.1 |
| 1 | 2 | price_x | 0.2 |
| 2 | 1 | price_y | 0.4 |
| 3 | 2 | price_y | 0.5 |
| 4 | 1 | price_z | 0.7 |
| 5 | 2 | price_z | 0.8 |
[5]:
spread(long, "measure", "value")
[5]:
| id | price_x | price_y | price_z | |
|---|---|---|---|---|
| 0 | 1 | 0.1 | 0.4 | 0.7 |
| 1 | 2 | 0.2 | 0.5 | 0.8 |
[6]:
one_missing = long[:-1]
spread(one_missing, "measure", "value")
[6]:
| id | price_x | price_y | price_z | |
|---|---|---|---|---|
| 0 | 1 | 0.1 | 0.4 | 0.7 |
| 1 | 2 | 0.2 | 0.5 | NaN |
[7]:
spread(one_missing, "measure", "value", fill = 99)
[7]:
| id | price_x | price_y | price_z | |
|---|---|---|---|---|
| 0 | 1 | 0.1 | 0.4 | 0.7 |
| 1 | 2 | 0.2 | 0.5 | 99.0 |
Edit page on github here.
Interactive version: