import pandas as pd

读取数据表

df = pd.read_csv('data.csv')

转换数据表

df_pivot = df.pivot_table(index=['SubstanceName', 'COUNTY'], columns='YYYY', values='DrugReports', fill_value=0).reset_index()

排序列

df_pivot = df_pivot.sort_values(by=['SubstanceName', 'COUNTY'])

恢复原数据表

df_recovered = df_pivot.melt(id_vars=['SubstanceName', 'COUNTY'], var_name='YYYY', value_name='DrugReports')

检验结果是否一致

result = df.equals(df_recovered) print(result)

Drug Report Trends by County and Substance: 2010-2017

原文地址: https://www.cveoy.top/t/topic/fvz4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录