Deepak Bhatt
Deepak Bhatt

Reputation: 39

ValueError: Length mismatch: Expected axis has 2 elements, new values have 3 elements

This is my code that I plan to use for creating a pie chart.

import csv
with open('C:\\Users\Bhuwan Bhatt\Desktop\IP PROJECT\Book1.csv', 'r') as file :
    reader = csv.reader(file)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

def piechart1():
   df=pd.read_csv('data,csv',  sep=' ', index_col=False,skipinitialspace=True\
                 ,error_bad_lines=False,encoding= 'unicode_escape')
   df=df.set_index(['Country'])
   dfl=df.iloc[:,[14]]
   final_df=dfl.sort_values(by='TotalMedal')
   final_df.reset_index(inplace=True)
   final_df.columns=('location','Total cases','Total Deaths')
   final_df=final_df.drop(11,axis='index')
   countries=df['Country']
   tmedals=df['TotalMedal']
   plt.pie(tmedals,labels=countries,explode=(0.1,0,0,0,0,0,0,0,0,0,0.2),shadow=True,autopct='%0.1f%%')
   plt.title("Olympics data analysis\nTop 10 Countries", color='b',fontsize=12)
   plt.gcf().canva.set_window_title("OLMPICS ANALYSIS")
   plt.show()

I get this error for some reason:

AttributeError: 'DataFrameGroupBy' object has no attribute 'sort_values'

This is the CSV file I've been using:

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

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

Upvotes: 1

Views: 10307

Answers (1)

Abhilash Awasthi
Abhilash Awasthi

Reputation: 797

In your code you set Country as Index and in this line

dfl=df.iloc[:,[14]]

you just pick one column which is TotalMedal.
After sorting and resetting index, you try to change column names by line

final_df.columns=('location','Total cases','Total Deaths')

Here is the error..you have filtered your dataframe for just one column and after resetting gets Country also in column. So you just have two columns in your dataframe and trying to change names of columns by providing three values.

Correct line could be -

final_df.columns=('location','TotalMedal')

Upvotes: 1

Related Questions