#πŸ”’ anyone help me convert this columns to certains types

25 messages Β· Page 1 of 1 (latest)

candid wolf
#

i've been trying to convert the last 3 columns to int type but nothing seems to work to me

fervent jackalBOT
#

@candid wolf

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

clever relic
#

how were you trying?

candid wolf
#

pd.to_numeric(df1['peak'])

#

df1['peak'].astype(int)

clever relic
#

And what happens?

candid wolf
#

didn't work

clever relic
#

That's not very descriptive. Do you get an error?

candid wolf
#

here is my code ```py
for i in ['peak', 'online', 'follows']:
df1[i] = df1[i].str.replace(',', '').str.replace('β€”', '')
df1[i] = df1[i].astype(int)

#

the column 'peak' converted to int successfuly

#

but the other failed

#

i guess because of the empty raws

clever relic
#

The error actually sounds like one of these columns is already numeric, rather. What does df1.info() say?

candid wolf
clever relic
#

Well, because peak already got converted to int, you can't use this piece of code again because it'll fail on trying to do df1["peak"].str.

#

As for the empty rows, to represent them you could replace them with pd.NA and then do .astype("Int64") - that's a pandas dtype that allows NA values.

candid wolf
#

it remains as an object type

clever relic
#

.astype isn't an inplace operation. You need to assign the new column back to df1 or you're just discarding it.

fervent jackalBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.