Python 将 Pandas 数据框中的一列从 int 转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17950374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a column within pandas dataframe from int to string
提问by Malfet
I have a dataframe in pandas with mixed int and str data columns. I want to concatenate first the columns within the dataframe. To do that I have to convert an int
column to str
.
I've tried to do as follows:
我在 Pandas 中有一个数据框,其中包含混合的 int 和 str 数据列。我想先连接数据框中的列。为此,我必须将一int
列转换为str
. 我试图做如下:
mtrx['X.3'] = mtrx.to_string(columns = ['X.3'])
or
或者
mtrx['X.3'] = mtrx['X.3'].astype(str)
but in both cases it's not working and I'm getting an error saying "cannot concatenate 'str' and 'int' objects". Concatenating two str
columns is working perfectly fine.
但在这两种情况下,它都不起作用,我收到一条错误消息,提示“无法连接 'str' 和 'int' 对象”。连接两str
列工作得很好。
回答by Jeff
In [16]: df = DataFrame(np.arange(10).reshape(5,2),columns=list('AB'))
In [17]: df
Out[17]:
A B
0 0 1
1 2 3
2 4 5
3 6 7
4 8 9
In [18]: df.dtypes
Out[18]:
A int64
B int64
dtype: object
Convert a series
转换系列
In [19]: df['A'].apply(str)
Out[19]:
0 0
1 2
2 4
3 6
4 8
Name: A, dtype: object
In [20]: df['A'].apply(str)[0]
Out[20]: '0'
Don't forget to assign the result back:
不要忘记将结果分配回来:
df['A'] = df['A'].apply(str)
Convert the whole frame
转换整个框架
In [21]: df.applymap(str)
Out[21]:
A B
0 0 1
1 2 3
2 4 5
3 6 7
4 8 9
In [22]: df.applymap(str).iloc[0,0]
Out[22]: '0'
df = df.applymap(str)
回答by tanaque
Change data type of DataFrame column:
更改 DataFrame 列的数据类型:
To int:
输入:
df.column_name = df.column_name.astype(np.int64)
df.column_name = df.column_name.astype(np.int64)
To str:
字符串:
df.column_name = df.column_name.astype(str)
df.column_name = df.column_name.astype(str)
回答by Keith
Warning: Both solutions given (astype() and apply() )do not preserve NULL values in either the nan or the None form.
警告:给出的两种解决方案(astype() 和 apply() )都不会以 nan 或 None 形式保留 NULL 值。
import pandas as pd
import numpy as np
df = pd.DataFrame([None,'string',np.nan,42], index=[0,1,2,3], columns=['A'])
df1 = df['A'].astype(str)
df2 = df['A'].apply(str)
print df.isnull()
print df1.isnull()
print df2.isnull()
I believe this is fixed by the implementation of to_string()
我相信这是通过to_string()的实现来解决的
回答by Faraz Ramtin
Use the following code:
使用以下代码:
df.column_name = df.column_name.astype('str')