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...
Answer from Andy Hayden on Stack Overflowpython - Convert columns to string in Pandas - Stack Overflow
Pandas: converting entire dataframe to string type, except for NaN entries
pandas dataframe - ValueError: could not convert string to float: 'None'
When debugging stuff like this, it helps to find a specific example where that triggers the error. Assuming your data has 100 rows, proceed as follows..
-
Does the error occur on the first element?
df.iloc[0].pct_used.astype(float) -
Does the error occur in the first half of the data?
df.iloc[:50].pct_used.astype(float)-
If yes, does the error occur in the first quarter of the data?
df.iloc[:25].pct_used.astype(float) -
If no, does the error occur in the second quarter of the data?
df.iloc[74:].pct_used.astype(float)
-
... continue until you find a specific example that triggers the error.
A possibly quicker technique would be to sort the data alphabetically and give it the ole eyeball inspection. df.pct_used.unique().sort_vaues()
pandas: convert strings to boolean columns
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)