0

Currently I have a dataframe with columns but for this purpose, I'll just list the columns of importance. Basically I have a dataframe like this:

id  type
1   1
2   1
3   2
4   2
5   1

But I want to format the integers such that

1 = 'One'
2 = 'Two'

But I don't want to change the int to a string like using pd.replace, but format it instead.

JNevill
  • 46,980
  • 4
  • 38
  • 63
Alban18
  • 1
  • 2
  • Is this what you're looking for? [`df.to_string`, `df.to_html`, or `df.style.format`](/a/36175424/4518341) – wjandrea Feb 14 '22 at 20:11
  • Can you solve this with a dictionary that you load or is that too much like `replace()`? I can't imagine there is a built in formatter to convert an integer to an english string representation like this... but it is python, so who knows. Also, I'm assuming possible entries aren't limited to `1` and `2`, right? – JNevill Feb 14 '22 at 20:13
  • `df["type"].map({1: "One", 2: "Two"})` – gold_cy Feb 14 '22 at 20:15
  • Does this answer your question? [Is there a simple way to change a column of yes/no to 1/0 in a Pandas dataframe?](https://stackoverflow.com/questions/40901770/is-there-a-simple-way-to-change-a-column-of-yes-no-to-1-0-in-a-pandas-dataframe) – gold_cy Feb 14 '22 at 20:16
  • `"But I don't want to change the int to a string"` so I don't think they want to `map` it either. The current question's wording is confusing. – tdy Feb 14 '22 at 20:18
  • Basically, I want the output to be this: id type 1 One 2 One 3 Two 4 Two 5 One But I want the type column to stay an int. Edit: I don't know how to make my output in stack to be easy to read, thanks to whoever made it readable. – Alban18 Feb 14 '22 at 21:00
  • Okay gold_cy, I'll try that – Alban18 Feb 14 '22 at 21:01
  • Well the map function pretty much does the same thing as the replace function. It turns the column that was an int to a Object which is basically a string. What I want is for it to stay an 'int64' but just format those values for output. – Alban18 Feb 14 '22 at 21:22

1 Answers1

0

you can actually use replace function on pandas. For example,

df['type'].replace(1, 'one')

then, you can try replace all the keyword such as for two, three, and so on.

One Attack
  • 103
  • 3