Convert columns to string in Pandas Convert columns to string in Pandas python python

Convert columns to string in Pandas


One way to convert to string is to use astype:

total_rows['ColumnID'] = total_rows['ColumnID'].astype(str)

However, perhaps you are looking for the to_json function, which will convert keys to valid json (and therefore your keys to strings):

In [11]: df = pd.DataFrame([['A', 2], ['A', 4], ['B', 6]])In [12]: df.to_json()Out[12]: '{"0":{"0":"A","1":"A","2":"B"},"1":{"0":2,"1":4,"2":6}}'In [13]: df[0].to_json()Out[13]: '{"0":"A","1":"A","2":"B"}'

Note: you can pass in a buffer/file to save this to, along with some other options...


If you need to convert ALL columns to strings, you can simply use:

df = df.astype(str)

This is useful if you need everything except a few columns to be strings/objects, then go back and convert the other ones to whatever you need (integer in this case):

 df[["D", "E"]] = df[["D", "E"]].astype(int) 


pandas >= 1.0: It's time to stop using astype(str)!

Prior to pandas 1.0 (well, 0.25 actually) this was the defacto way of declaring a Series/column as as string:

# pandas <= 0.25# Note to pedants: specifying the type is unnecessary since pandas will # automagically infer the type as objects = pd.Series(['a', 'b', 'c'], dtype=str)s.dtype# dtype('O')

From pandas 1.0 onwards, consider using "string" type instead.

# pandas >= 1.0s = pd.Series(['a', 'b', 'c'], dtype="string")s.dtype# StringDtype

Here's why, as quoted by the docs:

  1. You can accidentally store a mixture of strings and non-strings in an object dtype array. It’s better to have a dedicated dtype.

  2. object dtype breaks dtype-specific operations like DataFrame.select_dtypes(). There isn’t a clear way to select just textwhile excluding non-text but still object-dtype columns.

  3. When reading code, the contents of an object dtype array is less clear than 'string'.

See also the section on Behavioral Differences between "string" and object.

Extension types (introduced in 0.24 and formalized in 1.0) are closer to pandas than numpy, which is good because numpy types are not powerful enough. For example NumPy does not have any way of representing missing data in integer data (since type(NaN) == float). But pandas can using Nullable Integer columns.


Why should I stop using it?

Accidentally mixing dtypes
The first reason, as outlined in the docs is that you can accidentally store non-text data in object columns.

# pandas <= 0.25pd.Series(['a', 'b', 1.23])   # whoops, this should have been "1.23"0       a1       b2    1.23dtype: objectpd.Series(['a', 'b', 1.23]).tolist()# ['a', 'b', 1.23]   # oops, pandas was storing this as float all the time.
# pandas >= 1.0pd.Series(['a', 'b', 1.23], dtype="string")0       a1       b2    1.23dtype: stringpd.Series(['a', 'b', 1.23], dtype="string").tolist()# ['a', 'b', '1.23']   # it's a string and we just averted some potentially nasty bugs.

Challenging to differentiate strings and other python objects
Another obvious example example is that it's harder to distinguish between "strings" and "objects". Objects are essentially the blanket type for any type that does not support vectorizable operations.

Consider,

# Setupdf = pd.DataFrame({'A': ['a', 'b', 'c'], 'B': [{}, [1, 2, 3], 123]})df    A          B0  a         {}1  b  [1, 2, 3]2  c        123

Upto pandas 0.25, there was virtually no way to distinguish that "A" and "B" do not have the same type of data.

# pandas <= 0.25  df.dtypesA    objectB    objectdtype: objectdf.select_dtypes(object)   A          B0  a         {}1  b  [1, 2, 3]2  c        123

From pandas 1.0, this becomes a lot simpler:

# pandas >= 1.0# Convenience function I call to help illustrate my point.df = df.convert_dtypes()df.dtypesA    stringB    objectdtype: objectdf.select_dtypes("string")   A0  a1  b2  c

Readability
This is self-explanatory ;-)


OK, so should I stop using it right now?

...No. As of writing this answer (version 1.1), there are no performance benefits but the docs expect future enhancements to significantly improve performance and reduce memory usage for "string" columns as opposed to objects. With that said, however, it's never too early to form good habits!