pythonbook/实验 Pandas_速查表/pivot.py

20 lines
392 B
Python

import pandas as pd
import plotly.express as px
df = px.data.iris()
print(df.head(10))
df1 = pd.melt(df)
print(df1.tail(100))
data = {
'Date': ['2024-01-01', '2024-01-01', '2024-01-02', '2024-01-02'],
'Product': ['A', 'B', 'A', 'B'],
'Sales': [100, 150, 120, 110]
}
df = pd.DataFrame(data)
pivot_df = df.pivot(index='Date', columns='Product', values='Sales')
print(pivot_df)