0

I have a data frame with 6 columns. I would like to convert one of the columns (the second one) from float to int, when I do this it only returns the one column as an int but I want the exact same dataframe in its entirety, the only difference is that the second column is int as opposed to float.

I have checked all relevant questions on here and nothing worked for me!! please help

2
  • 5
    Does this answer your question? Convert floats to ints in Pandas? Commented Jun 15, 2020 at 5:16
  • The astype function in pandas might work here. Commented Jun 15, 2020 at 5:53

3 Answers 3

2

You can do something like this!

>>> df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
                       'num_wings': [2, 0, 0, 0],
                       'num_specimen_seen': [10, 2, 1, 8]},
                       index=['falcon', 'dog', 'spider', 'fish'])

>>> df
        num_legs  num_wings  num_specimen_seen
falcon         2          2                 10
dog            4          0                  2
spider         8          0                  1
fish           0          0                  8

>>> df['test'] = df['num_wings'].astype('float')

>>> df
        num_legs  num_wings  num_specimen_seen  test
falcon         2          2                 10   2.0
dog            4          0                  2   0.0
spider         8          0                  1   0.0
fish           0          0                  8   0.0

You can refer to this link for more details!

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, could you please include the link's contents in your answer?
No, include the link's content(whatever the website says)in your answer. This is helpful in case the website changes, or the link rots.
2

It seems like you have a dataframe that looks something like this:

>>> print(df)
  c1   c2  c3 c4  c5 c6
0  a  1.0   5  d  20  z
1  b  2.0   6  e  40  y
2  c  3.0   7  f  60  x

I believe you may be using DataFrame.astype to convert your column of floats to a column of ints. Specifically, it seems like you've gotten to this point:

>>> df['c2'].astype(int)
0    1
1    2
2    3

Now, all you need to do is replace the column by calling:

>>> df['c2'] = df['c2'].astype(int)
>>> print(df)
  c1  c2  c3 c4  c5 c6
0  a   1   5  d  20  z
1  b   2   6  e  40  y
2  c   3   7  f  60  x

If you'd like to create a new column of ints, you could do this instead:

>>> df['new_col'] = df['c2'].astype(int)
>>> print(df)
  c1  c2    c3 c4  c5 c6  new_col
0  a   1.0   5  d  20  z        1
1  b   2.0   6  e  40  y        2
2  c   3.0   7  f  60  x        3

2 Comments

While this code may answer the question, it's generally helpful to give some more detail as to what the code does.
You're right. I've updated the answer to include a more detailed explanation.
1

You can reassign to a new column or replace the same column values by

df["col1"] = df["col1"].astype('int32')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.