I've got a dataframe column GDP/year
from a dataset about suicides over some years. The data type of this column is currently object (string), but I want it as integer.
The values are commas separated so I can't directly transform them to integers. I tried string-removing the commas, storing as integer, then I introduce the commas again, but its the type reverts back to object.
The dataset: https://www.kaggle.com/russellyates88/suicide-rates-overview-1985-to-2016
# convert to int...
suicides[' gdp_for_year ($) '] = suicides[' gdp_for_year ($) '].str.replace(',','').astype(int)
# now reformat with commas as thousands separator...
suicides[' gdp_for_year ($) '] = suicides[' gdp_for_year ($) '].astype(int).apply(lambda x: "{:,}".format(x))
# ...wanted to get dtype integer, but it's back to object
you are converting to string each element : "{:,}".format(x)
but I guess you want to display your numbers in your pandas DataFrame to show comma separators by default, for this you can do it but for float data type:
pd.options.display.float_format = '{:,}'.format
if you want also for int type you should monkey-patch pandas.io.formats.format.IntArrayFormatter
.