1

I want change all strings in the pandas DF to the corresponding* integers. I can use this routine:

k=0
for item in df['someColumn'].unique():
    df.replace(item, k)
    k += 1

or even I can do list comprehension... Anyway I want to ask, does somebody know about probably some specific method in Pandas, which could replace ALL strings in some column with the corresponding (=different) integer (or float) values..? Please, propose only pythonic way.

Probably some additional aspect. My column doesn't have some numeric values in the string format, just really strings (= concatenation of the chars):

C
asd
bbd
ksl
asd
asd
ksl

I want

C 
1
2
3
1
1
3

*Probably the word corresponding is a little bit confusing here, I'm very sorry

Guforu
  • 3,835
  • 8
  • 33
  • 52
  • 1
    The correct method is to use [`convert_objects`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.convert_objects.html#pandas.DataFrame.convert_objects) so `df.convert_objects(convert_numeric=True)` – EdChum Sep 26 '14 at 13:48
  • something like `df['column'].astype(np.float)`? – acushner Sep 26 '14 at 13:49
  • 2
    you want to assign ints uniquely to the strings yes, see here: http://stackoverflow.com/questions/25963431/convert-pandas-series-from-string-to-unique-int-ids – Jeff Sep 26 '14 at 14:07
  • Sorry got the wrong end of the stick, Jeff has correctly pointed you to the canonical answer – EdChum Sep 26 '14 at 14:09
  • @Jeff, yes - exactly what I want. EdChum, thank you anyway :-) – Guforu Sep 26 '14 at 14:12
  • You should probably vote to close and add a link to that question as duplicate, question won't be deleted so other users will be able to find the questions linking to same answer – EdChum Sep 26 '14 at 14:15
  • ok, I'm agree, how I can vote here? I voted only for Jeff's answer – Guforu Sep 26 '14 at 14:19
  • Can you see the 'close' button underneath your question? They'll be under the tags. If not you may not have enough rep to do so, if not don't worry and just move on. – EdChum Sep 26 '14 at 14:26

0 Answers0