@ Founded in :2022.07.01
@ Modified to :2022.07.01
import pandas as pd
import numpy as np
df = pd.DataFrame({
' full name ': ['name_A', 'name_B', 'name_C'],
' class ': ['c1', 'c2', 'c2'],
' Chinese language and literature ': [90, 60, 70],
' mathematics ': [80, 98, 80],
' English ': [85, 90, 75],
' Physics ': [92, 63, 76],
' chemical ': [85,100, 89],
' biological ': [87, 91, 80]})
be used pandas Of melt Method .
# One row to many columns
df_c = pd.melt(df, id_vars=[' full name ', ' class '], var_name=" subject ", value_name=" score ")
df_c
pd.pivot(df_c, index=" full name ", columns=" subject ", values=" score ")
# rename_axis(index=, columns=) To rename the axis
pd.pivot(df_c, index=" full name ", columns=" subject ", values=" score ").rename_axis(columns=None).reset_index()
The class was lost above , How to increase classes ?
pd.pivot(df_c, index=[" full name ", ' class '], columns=" subject ", values=" score ").rename_axis(columns=None).reset_index()
pandas Transfer line column 、 Column turned 、 And one row generates multiple rows
pandas How to perform elegant column to row conversion 、 Transfer line column ?
Pandas in Dataframe Transfer line column