Convert columns to string in Pandas
dataframe, pandas, python, string, type-conversion
Solution
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...
Problem
I have the following DataFrame from a SQL query: ``` (Pdb) pp total_rows ColumnID RespondentCount 0 -1 2 1 3030096843 1 2 3030096845 1 ``` and I pivot it like this: ``` total_data = total_rows.pivot_table(cols=['ColumnID']) ``` which produces ``` (Pdb) pp total_data ColumnID -1 3030096843 3030096845 RespondentCount 2 1 1 [1 rows x 3 columns] ``` When I convert this dataframe into a dictionary (using `total_data.to_dict('records')[0]`), I get ``` {3030096843: 1, 3030096845: 1, -1: 2} ``` but I want to make sure the 303 columns are cast as strings instead of integers so that I get this: ``` {'3030096843': 1, '3030096845': 1, -1: 2} ```