我有以下数据帧从一个SQL查询:

(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列被转换为字符串而不是整数,这样我就得到了这个:

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

当前回答

这是另一个,特别有用的多列转换为字符串,而不是单一的列:

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

其他回答

如果你需要将所有列转换为字符串,你可以简单地使用:

df = df.astype(str)

如果你需要除了少数列以外的所有列都是字符串/对象,那么返回并将其他列转换为你需要的任何东西(在这种情况下是整数),这是非常有用的:

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

有四种方法将列转换为字符串

1. astype(str)
df['column_name'] = df['column_name'].astype(str)

2. values.astype(str)
df['column_name'] = df['column_name'].values.astype(str)

3. map(str)
df['column_name'] = df['column_name'].map(str)

4. apply(str)
df['column_name'] = df['column_name'].apply(str)

让我们看看每种类型的性能

#importing libraries
import numpy as np
import pandas as pd
import time

#creating four sample dataframes using dummy data
df1 = pd.DataFrame(np.random.randint(1, 1000, size =(10000000, 1)), columns =['A'])
df2 = pd.DataFrame(np.random.randint(1, 1000, size =(10000000, 1)), columns =['A'])
df3 = pd.DataFrame(np.random.randint(1, 1000, size =(10000000, 1)), columns =['A'])
df4 = pd.DataFrame(np.random.randint(1, 1000, size =(10000000, 1)), columns =['A'])

#applying astype(str)
time1 = time.time()
df1['A'] = df1['A'].astype(str)
print('time taken for astype(str) : ' + str(time.time()-time1) + ' seconds')

#applying values.astype(str)
time2 = time.time()
df2['A'] = df2['A'].values.astype(str)
print('time taken for values.astype(str) : ' + str(time.time()-time2) + ' seconds')

#applying map(str)
time3 = time.time()
df3['A'] = df3['A'].map(str)
print('time taken for map(str) : ' + str(time.time()-time3) + ' seconds')

#applying apply(str)
time4 = time.time()
df4['A'] = df4['A'].apply(str)
print('time taken for apply(str) : ' + str(time.time()-time4) + ' seconds')

输出

time taken for astype(str): 5.472359895706177 seconds
time taken for values.astype(str): 6.5844292640686035 seconds
time taken for map(str): 2.3686647415161133 seconds
time taken for apply(str): 2.39758563041687 seconds

Map (str)和apply(str)与其余两种技术相比需要更少的时间

熊猫版本:1.3.5

更新后的答案

Df ['colname'] = Df ['colname'].astype(str) =>默认情况下这应该有效。但是如果你在使用astype(str)之前创建了str变量,比如str = "myString",这将不起作用。在本例中,您可能想使用下面的行。

[‘科尔’]

===========

(注:不正确的旧解释)

Df ['colname'] = Df ['colname'].astype('str') =>将数据帧列转换为字符串类型

等等,等等,等等

转换为字符串的一种方法是使用astype:

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

然而,也许你正在寻找to_json函数,它将键转换为有效的json(因此你的键转换为字符串):

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

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

我通常用这个:

pd['Column'].map(str)