Home > Net >  How to solve ValueError: DataFrame constructor not properly called
How to solve ValueError: DataFrame constructor not properly called

Time:09-21

I have tried to put the csv data into pandas data frame but i am getting an error "DataFrame constructor not properly called!". i have uploaded csv file on the github. file="https://raw.githubusercontent.com/gambler2020/Data_Analysis/master/Economy/WEO_Data.csv"

with open("WEO_Data.csv", encoding='utf-16') as f:
  contents = f.read()
df = pd.DataFrame(contents)

ValueError: DataFrame constructor not properly called!

How will i solve this error.

CodePudding user response:

You should just use pd.read_csv to do the work.

df = pd.read_csv("WEO_Data.csv")

If you are planning to do amendments in the data, you can do so after reading it into Pandas dataframe.

CodePudding user response:

Use read_csv with encoding='utf-16' parameter:

file="https://raw.githubusercontent.com/gambler2020/Data_Analysis/master/Economy/WEO_Data.csv"

df = pd.read_csv(file, encoding='utf-16')

print (df.head())
       Country    1980    1981    1982    1983    1984    1985    1986  \
0  Afghanistan     NaN     NaN     NaN     NaN     NaN     NaN     NaN   
1      Albania   1.946   2.229   2.296   2.319   2.290   2.339   2.587   
2      Algeria  42.346  44.372  44.780  47.529  51.513  61.132  61.535   
3      Andorra     NaN     NaN     NaN     NaN     NaN     NaN     NaN   
4       Angola   6.639   6.214   6.214   6.476   6.864   8.457   7.918   

     1987    1988    1989    1990    1991    1992    1993    1994    1995  \
0     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN   
1   2.566   2.530   2.779   2.221   1.333   0.843   1.461   2.361   2.882   
2  63.300  51.664  52.558  61.892  46.670  49.217  50.963  42.426  42.066   
3     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN   
4   9.050   9.818  11.421  12.571  12.186   9.395   6.819   4.965   6.197   

     1996    1997    1998    1999    2000    2001    2002    2003    2004  \
0     NaN     NaN     NaN     NaN     NaN     NaN   4.367   4.553   5.146   
1   3.200   2.259   2.560   3.209   3.483   3.928   4.348   5.611   7.185   
2  46.941  48.178  48.188  48.845  54.749  54.745  56.761  67.864  85.332   
3     NaN     NaN     NaN     NaN   1.429   1.547   1.758   2.362   2.896   
4   7.994   9.388   7.958   7.526  11.166  10.930  15.286  17.813  23.552   

...
  • Related