[PYTHON] Use pandas to convert grid data to row-holding (?) Data

Note that I was worried when I wanted to convert the grid data.

Thing you want to do

There is such grid data

import pandas as pd

df = pd.DataFrame(
    np.arange(9).reshape(3, 3),
    index=["y01", "y02", "y03"],
    columns=["x01", "x02", "x03"]
)
x01 x02 x03
y01 0 1 2
y02 3 4 5
y03 6 7 8

I want to convert this like this.

column0 column1 column2
y01 x01 0
y01 x02 1
y01 x03 2
y02 x01 3
y02 x02 4
y02 x03 5
y03 x01 6
y03 x02 7
y03 x03 8

Solution

In pandas, you can use the following method.

df.stack().reset_index()

Then it comes out like this.

level_0 level_1 0
0 y01 x01 0
1 y01 x02 1
2 y01 x03 2
3 y02 x01 3
4 y02 x02 4
5 y02 x03 5
6 y03 x01 6
7 y03 x02 7
8 y03 x03 8

Happy !! Please let me know if there is a better way

Recommended Posts

Use pandas to convert grid data to row-holding (?) Data
How to use Pandas 2
Convert 202003 to 2020-03 with pandas
[Python] Convert list to Pandas [Pandas]
How to use Pandas Rolling
[Python] How to use Pandas Series
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Python] Use pandas to extract △△ that maximizes ○○
Convert xml format data to txt format data (yolov3)
[Pandas] What is set_option [How to use]
[MNIST] Convert data to PNG for keras
Convert FX 1-minute data to 5-minute data with Python
Try converting to tidy data with pandas
How to use "deque" for Python data
How to use pandas Timestamp and date_range
Convert data with shape (number of data, 1) to (number of data,) with numpy.
Convert to HSV
Convert Mobile Suica usage history PDF to pandas Data Frame format with tabula-py
Read pandas data
Use decorators to prevent re-execution of data processing
How to use data analysis tools for beginners
Convert json format data to txt (using yolo)
Use a cool graph to analyze PES data!
Convert pandas dataframe elements to regular string type
Use Pandas to write only the specified lines of the data frame to an excel file
I convert AWS JSON data to CSV like this
How to use xgboost: Multi-class classification with iris data
I want to give a group_id to a pandas data frame
How to extract non-missing value nan data with pandas
How to convert JSON file to CSV file with Python Pandas
C> Convert decimal to hexadecimal> Do not use sprintf ()
I tried to summarize how to use pandas in python
Convert numeric variables to categorical with thresholds in pandas
How to extract non-missing value nan data with pandas
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use image-match
How to use shogun
How to use Virtualenv
How to use numpy.vectorize
Convert kanji to kana
How to use pytest_report_header
Easy to use Flask
Convert jupyter to py
How to use partial
How to use Bio.Phylo
Convert keras-yolo3 to onnx
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
Convert dict to array
Data visualization with pandas
How to use iptables
How to use numpy