I have a column of float values in a Pandas data frame like so:
df['nums'] = [100.0, 35000.00, 42639.25, 552.27]
And I want to convert whole number floats like 100.0
into integer data type.
I know I can access the last two digits of the list using str(df['nums'][1])[-2:]
which should print '.0'
.
I tried something like:
for i, row in enumerate(df.itertuples(), 1):
if str(row[0])[-2:] == '.0':
row[0] = row[0].astype(int)
else:
continue
Of course, this doesn't work in the slightest. How can I iterate over this column in a way that it can convert every whole number float into an int while keeping non-whole number floats intact? df['nums']
would look like this:
df['nums'] = [100, 35000, 42639.25, 552.27]
.
Thanks!