将列转换为pandas中的string

我有一个SQL查询中的以下DataFrame:

(Pdb) pp total_rows ColumnID RespondentCount 0 -1 2 1 3030096843 1 2 3030096845 1 

我想要这样做:

 total_data = total_rows.pivot_table(cols=['ColumnID']) (Pdb) pp total_data ColumnID -1 3030096843 3030096845 RespondentCount 2 1 1 [1 rows x 3 columns] total_rows.pivot_table(cols=['ColumnID']).to_dict('records')[0] {3030096843: 1, 3030096845: 1, -1: 2} 

但我想确保303列被铸造成string,而不是整数,所以我得到这个:

 {'3030096843': 1, '3030096845': 1, -1: 2} 

一种转换为string的方法是使用astype :

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

但是,也许你正在寻找to_json函数,它将键转换为有效的json(因此你的键到string):

 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"}' 

注意:你可以传入一个缓冲区/文件来保存它,以及其他一些选项…

下面是另外一个, 将多列转换为string而不是单列的情况特别有用

 In [76]: import numpy as np In [77]: import pandas as pd In [78]: df = pd.DataFrame({ ...: 'A': [20, 30.0, np.nan], ...: 'B': ["a45a", "a3", "b1"], ...: 'C': [10, 5, np.nan]}) ...: In [79]: df.dtypes ## Current datatype Out[79]: A float64 B object C float64 dtype: object ## Multiple columns string conversion In [80]: df[["A", "C"]] = df[["A", "C"]].astype(str) In [81]: df.dtypes ## Updated datatype after string conversion Out[81]: A object B object C object dtype: object