Home > Enterprise >  Converting Pandas Dataframe to Numpy array with to_numpy() doesn't change my output at all
Converting Pandas Dataframe to Numpy array with to_numpy() doesn't change my output at all

Time:08-01

I'm trying to write a function that converts the following dataframe to a numpy array but the output is not changing to an array, can someone assist? Please find the input dataframe, my code, my output and expected output below:

Function parameters: Should take a str as input and return a numpy array type as output. The array should only have two columns containing the year and the population, in other words, it should have a shape (?, 2) where ? is the length of the data. The values within the array should be of type int.

joined_df my code:

def year_pop(country_name):
    return get_years

Input:

year_pop('Aruba')

Expected output:

array([[  1960,  54211],
       [  1961,  55438],
       [  1962,  56225],
        ...
       [  2016, 104822],
       [  2017, 105264]])

My output after code

CodePudding user response:

to_numpy method does not work inplace, so you need to assign the result to get_years to overwrite the dataframe with the numpy array:

get_years = get_years.to_numpy(dtype=int)

If you want to reshape it as in the expected output:

get_years = get_years.T.reset_index().to_numpy(dtype=int)
  • Related