Command to call non numeric value from a dataset?

Viewed 20

New to Python, as part of an assessment I need to collect data samples from a dataset, the information has been put through a labelencoder with:

 le = LabelEncoder()
 for i in columns:
     #print(i)
     data[i] = le.fit_transform(data[i])


 data.head()

this shows the below table.

data info

if i use the command:

data['native-country'].value_counts()

I will get numerical values when at this point I want to see the actual country rather than the numerical value assigned. how do I do this?

thanks.

1 Answers

The funcion value_counts returns a series with values as index entries. Since values in the dataframe are numbers, that's what you get.

You can use the library phone_iso3166 to lookup the numeric country codes (I assume they're telephone prefixes) and update the index

df
# Out: 
#    col_1  native_country
# 0      3              39
# 1      2              39
# 2      1              20

vc = df['native_country'].value_counts()

vc
# 39    2
# 20    1
# Name: native_country, dtype: int64

Import library and lookup country code

from phone_iso3166.country import *    
vc.to_frame().set_index(vc.index.map(phone_country))
# Out: 
#     native_country
# IT               2
# EG               1

vc.to_frame().set_index(vc.index.map(phone_country)) \
             .rename(columns={'native_country':'count'})
# Out: 
#     count
# IT      2
# EG      1

Or just use any other feasible dictionary/library for converting the codes to country names.

Related