Pandas线图

2024-04-29 00:37:03 发布

您现在位置:Python中文网/ 问答频道 /正文

从2000年到2004年,我有以下数据显示了各州的专利信息

States;2000;2001;2002;2003;2004
state1;12302;11610;12297;13291;13491
state2;13010;14304;13599;13761;14398
state3;1242;1228;1099;1109;1016
state4;398;399;379;408;361
state5;164;198;143;158;185
state6;1185;1427;1166;955;1105

我正在使用pandas将这些数据加载到数据帧中

patents = pd.read_csv('Patent.csv', encoding='ANSI', 
                     sep=';', header=0, skiprows=0, 
                     names=['States','Year'],
                     index_col=['States'])

但我不确定我做的是否正确,因为我的数据有两个标题。(州和年份) 当我使用

patents.plot(kind='line')

它不显示各州和年份之间专利数量的折线图:

enter image description here

谢谢你的帮助


Tags: csv数据信息pandas专利pd年份states
3条回答

你有5年,但你只给一个名字。 您需要有5年列:

patents = pd.read_csv('Patent.csv', encoding='ANSI', 
    sep=';', header=0, skiprows=0, 
    names=['States','2000','2001','2002','2003','2004'],
    index_col=['States'])

或将数据更改为字符串:

States;Years
state1;"12302,11610,12297,13291,13491"
state2;"13010,14304,13599,13761,14398"

我想你想要这样的东西:

patents.set_index('States').T.plot(kind='line')

输出:

enter image description here

同时尝试:

patents =pd.read_csv('patents.csv',sep=';',encoding='ANSI',index_col='States')
patents .transpose().plot(kind='line')
patents .transpose().plot(kind='bar')

相关问题 更多 >