長寬表轉換¶
使用flights資料集來說明Long Form跟Wide Form。
In [ ]:
Copied!
import pandas as pd
import plotly.express as px
import pandas as pd
import plotly.express as px
In [ ]:
Copied!
url = "https://github.com/mwaskom/seaborn-data/raw/master/flights.csv"
url = "https://github.com/mwaskom/seaborn-data/raw/master/flights.csv"
In [ ]:
Copied!
df = pd.read_csv(url)
df = pd.read_csv(url)
In [ ]:
Copied!
df
df
Out[ ]:
year | month | passengers | |
---|---|---|---|
0 | 1949 | January | 112 |
1 | 1949 | February | 118 |
2 | 1949 | March | 132 |
3 | 1949 | April | 129 |
4 | 1949 | May | 121 |
... | ... | ... | ... |
139 | 1960 | August | 606 |
140 | 1960 | September | 508 |
141 | 1960 | October | 461 |
142 | 1960 | November | 390 |
143 | 1960 | December | 432 |
144 rows × 3 columns
使用pivot將long form的資料轉成wide form的資料¶
In [ ]:
Copied!
wide = df.pivot(index='year',columns='month',values='passengers')
wide = df.pivot(index='year',columns='month',values='passengers')
In [ ]:
Copied!
wide
wide
Out[ ]:
month | April | August | December | February | January | July | June | March | May | November | October | September |
---|---|---|---|---|---|---|---|---|---|---|---|---|
year | ||||||||||||
1949 | 129 | 148 | 118 | 118 | 112 | 148 | 135 | 132 | 121 | 104 | 119 | 136 |
1950 | 135 | 170 | 140 | 126 | 115 | 170 | 149 | 141 | 125 | 114 | 133 | 158 |
1951 | 163 | 199 | 166 | 150 | 145 | 199 | 178 | 178 | 172 | 146 | 162 | 184 |
1952 | 181 | 242 | 194 | 180 | 171 | 230 | 218 | 193 | 183 | 172 | 191 | 209 |
1953 | 235 | 272 | 201 | 196 | 196 | 264 | 243 | 236 | 229 | 180 | 211 | 237 |
1954 | 227 | 293 | 229 | 188 | 204 | 302 | 264 | 235 | 234 | 203 | 229 | 259 |
1955 | 269 | 347 | 278 | 233 | 242 | 364 | 315 | 267 | 270 | 237 | 274 | 312 |
1956 | 313 | 405 | 306 | 277 | 284 | 413 | 374 | 317 | 318 | 271 | 306 | 355 |
1957 | 348 | 467 | 336 | 301 | 315 | 465 | 422 | 356 | 355 | 305 | 347 | 404 |
1958 | 348 | 505 | 337 | 318 | 340 | 491 | 435 | 362 | 363 | 310 | 359 | 404 |
1959 | 396 | 559 | 405 | 342 | 360 | 548 | 472 | 406 | 420 | 362 | 407 | 463 |
1960 | 461 | 606 | 432 | 391 | 417 | 622 | 535 | 419 | 472 | 390 | 461 | 508 |
In [ ]:
Copied!
px.imshow(wide, title='旅客人數')
px.imshow(wide, title='旅客人數')
In [ ]:
Copied!
px.imshow(wide, text_auto=True, title='旅客人數')
px.imshow(wide, text_auto=True, title='旅客人數')
使用melt將wide form的資料轉成long form的資料¶
In [ ]:
Copied!
df.melt?
df.melt?
In [ ]:
Copied!
long = wide.reset_index().melt('year', var_name='month', value_name='passengers')
long = wide.reset_index().melt('year', var_name='month', value_name='passengers')
In [ ]:
Copied!
long
long
Out[ ]:
year | month | passengers | |
---|---|---|---|
0 | 1949 | April | 129 |
1 | 1950 | April | 135 |
2 | 1951 | April | 163 |
3 | 1952 | April | 181 |
4 | 1953 | April | 235 |
... | ... | ... | ... |
139 | 1956 | September | 355 |
140 | 1957 | September | 404 |
141 | 1958 | September | 404 |
142 | 1959 | September | 463 |
143 | 1960 | September | 508 |
144 rows × 3 columns
In [ ]:
Copied!