0

I would like to iterate through a dataframe and create a new column with the indice returned by enumerate(), but I'm unable to assign the value as integer and I need to convert it later. Is there a solution to do that in one shot ?

As you can see, direct assignment of an integer fails.

df.loc[indexes, ('route', 'id')] = int(i)
print(df.loc[indexes, ('route', 'id')].dtypes)  # float64

Conversion with a second line of code is necessary:

df.loc[indexes, ('route', 'id')] = df.loc[indexes, ('route', 'id')].astype(int)
print(df.loc[indexes, ('route', 'id')].dtypes)  # int64
Florent
  • 1,791
  • 22
  • 40
  • 2
    If you assign an integer to a particular cell, the column that cell is in has NaN everywhere else. Because of that, it can only be represented in float64. – Nick ODell May 25 '21 at 17:25

1 Answers1

1

This link shows you how to assing the value of a column to an int type in pandas. Basically you can do this by using:

  1. to_numeric() - provides functionality to safely convert non-numeric types (e.g. strings) to a suitable numeric type. (See also to_datetime() and to_timedelta().)
  2. astype() - convert (almost) any type to (almost) any other type (even if it's not necessarily sensible to do so). Also allows you to convert to categorial types (very useful).
  3. infer_objects() - a utility method to convert object columns holding Python objects to a pandas type if possible.
  4. convert_dtypes() - convert DataFrame columns to the "best possible" dtype that supports pd.NA (pandas' object to indicate a missing value).
drauedo
  • 641
  • 4
  • 17