ValueError长度不匹配预期轴有2个元素,新值有3个元素

2024-09-30 14:23:35 发布

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

def bar1():
df=pd.read_csv('#CSVFILELOCATION#',encoding= 'unicode_escape')
x=np.arange(11)
df=df.set_index(['Country'])
dfl=df.iloc[:,[4,9]]
w=dfl.groupby('Country')['SummerTotal' , 'WinterTotal'].sum()
final_df=w.sort_values(by='Country').tail(11)
final_df.reset_index(inplace=True)
final_df.columns=('Country','SummerTotal','WinterTotal')
final_df=final_df.drop(11,axis='index')
Countries=df['Country']
STotalMed=df['SummerTotal']
WTotalMed=df['WinterTotal']
plt.bar(x-0.25,STotalMed,label='Total Medals by Countries in Summer',color='g')
plt.bar(x+0.25,WTotalMed,label='Total Medals by Countries in Winter',color='r')
plt.xticks(r,Countries,rotation=30)
plt.title('Olympics Data Analysis of Top 10 Countries',color='red',fontsize=10)
plt.xlabel('Countries')
plt.ylabel('Total Medals')
plt.grid()
plt.legend()
plt.show()

这是我在项目中使用的条形图的代码 这里有一个错误

ValueError:长度不匹配:预期轴有2个元素,新值有3个元素

请帮助任何人我想提交这个项目快

CSV:

Country SummerTimesPart Sumgoldmedal    Sumsilvermedal  Sumbronzemedal  SummerTotal WinterTimesPart Wingoldmedal    Winsilvermedal  Winbronzemedal  WinterTotal TotalTimesPart  Tgoldmedal  Tsilvermedal    Tbronzemedal    TotalMedal
 Afghanistan    14  0   0   2   2   0   0   0   0   0   14  0   0   2   2
 Algeria    13  5   4   8   17  3   0   0   0   0   16  5   4   8   17
 Argentina  24  21  25  28  74  19  0   0   0   0   43  21  25  28  74
 Armenia    6   2   6   6   14  7   0   0   0   0   13  2   6   6   14
 Australasia    2   3   4   5   12  0   0   0   0   0   2   3   4   5   12
 Australia  26  147 163 187 497 19  5   5   5   15  45  152 168 192 512
 Austria    27  18  33  36  87  23  64  81  87  232 50  82  114 123 319
 Azerbaijan     6   7   11  24  42  6   0   0   0   0   12  7   11  24  42
 Bahamas    16  6   2   6   14  0   0   0   0   0   16  6   2   6   14
 Bahrain    9   2   1   0   3   0   0   0   0   0   9   2   1   0   3
 Barbados   12  0   0   1   1   0   0   0   0   0   12  0   0   1   1
 Belarus    6   12  27  39  78  7   8   5   5   18  13  20  32  44  96
 Belgium    26  40  53  55  148 21  1   2   3   6   47  41  55  58  154
 Bermuda    18  0   0   1   1   8   0   0   0   0   26  0   0   1   1
 Bohemia    3   0   1   3   4   0   0   0   0   0   3   0   1   3   4
 Botswana   10  0   1   0   1   0   0   0   0   0   10  0   1   0   1
 Brazil     22  30  36  63  129 8   0   0   0   0   30  30  36  63  129
 British West Indies    1   0   0   2   2   0   0   0   0   0   1   0   0   2   2
 Bulgaria   20  51  87  80  218 20  1   2   3   6   40  52  89  83  224
 Burundi    6   1   1   0   2   0   0   0   0   0   6   1   1   0   2
 Cameroon   14  3   1   2   6   1   0   0   0   0   15  3   1   2   6


INFO-----> SummerTimesPart  :  No. of times participated in summer by each country
           WinterTimesPart  :  No. of times participated in winter by each country
     

Tags: ofindfindexbypltcountrycountries
1条回答
网友
1楼 · 发布于 2024-09-30 14:23:35

要使图表正常工作,需要进行一些更改:

  • 绘制国家/地区名称需要勾号数组
  • 对图表数据使用final_df,而不是df
  • 设置钢筋宽度,使钢筋不重叠

以下是更新的代码:

data = '''
Country SummerTimesPart Sumgoldmedal Sumsilvermedal Sumbronzemedal SummerTotal WinterTimesPart Wingoldmedal Winsilvermedal Winbronzemedal WinterTotal TotalTimesPart Tgoldmedal Tsilvermedal Tbronzemedal TotalMedal
Afghanistan 14 0 0 2 2 0 0 0 0 0 14 0 0 2 2
Algeria 13 5 4 8 17 3 0 0 0 0 16 5 4 8 17
Argentina 24 21 25 28 74 19 0 0 0 0 43 21 25 28 74
Armenia 6 2 6 6 14 7 0 0 0 0 13 2 6 6 14
Australasia 2 3 4 5 12 0 0 0 0 0 2 3 4 5 12
Australia 26 147 163 187 497 19 5 5 5 15 45 152 168 192 512
Austria 27 18 33 36 87 23 64 81 87 232 50 82 114 123 319
Azerbaijan 6 7 11 24 42 6 0 0 0 0 12 7 11 24 42
Bahamas 16 6 2 6 14 0 0 0 0 0 16 6 2 6 14
Bahrain 9 2 1 0 3 0 0 0 0 0 9 2 1 0 3
Barbados 12 0 0 1 1 0 0 0 0 0 12 0 0 1 1
Belarus 6 12 27 39 78 7 8 5 5 18 13 20 32 44 96
Belgium 26 40 53 55 148 21 1 2 3 6 47 41 55 58 154
Bermuda 18 0 0 1 1 8 0 0 0 0 26 0 0 1 1
Bohemia 3 0 1 3 4 0 0 0 0 0 3 0 1 3 4
Botswana 10 0 1 0 1 0 0 0 0 0 10 0 1 0 1
Brazil 22 30 36 63 129 8 0 0 0 0 30 30 36 63 129
BritishWestIndies 1 0 0 2 2 0 0 0 0 0 1 0 0 2 2
Bulgaria 20 51 87 80 218 20 1 2 3 6 40 52 89 83 224
Burundi 6 1 1 0 2 0 0 0 0 0 6 1 1 0 2
Cameroon 14 3 1 2 6 1 0 0 0 0 15 3 1 2 6
'''.strip()

with open('data,csv', 'w') as f: f.write(data)  # write test file


############################
   

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

def bar1():
    df=pd.read_csv('data,csv', encoding= 'unicode_escape', sep=' ', index_col=False)
    x=np.arange(11)
    df=df.set_index(['Country'])
    dfl=df.iloc[:,[4,9]]
    w=dfl.groupby('Country')['SummerTotal' , 'WinterTotal'].sum()
    final_df=w.sort_values(by='Country').tail(11)
    final_df.reset_index(inplace=True)
    final_df.columns=('Country','SummerTotal','WinterTotal')
    print(final_df)
#    final_df=final_df.drop(11,axis='index')
    Countries=final_df['Country']
    STotalMed=final_df['SummerTotal']
    WTotalMed=final_df['WinterTotal']
    plt.bar(x-0.25,STotalMed,width=.2, label='Total Medals by Countries in Summer',color='g')
    plt.bar(x+0.25,WTotalMed,width=.2, label='Total Medals by Countries in Winter',color='r')
    plt.xticks(np.arange(11),Countries,rotation=30)
    plt.title('Olympics Data Analysis of Top 10 Countries',color='red',fontsize=10)
    plt.xlabel('Countries')
    plt.ylabel('Total Medals')
    plt.grid()
    plt.legend()
    plt.show()
    
bar1()

输出

Chart

相关问题 更多 >