Python 在 Pandas 中将列转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22005911/
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
Convert Columns to String in Pandas
提问by sontek
I have the following DataFrame from a SQL query:
我有来自 SQL 查询的以下 DataFrame:
(Pdb) pp total_rows
ColumnID RespondentCount
0 -1 2
1 3030096843 1
2 3030096845 1
and I want to pivot it like this:
我想像这样旋转它:
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}
but I want to make sure the 303 columns are casted as strings instead of integers so that I get this:
但我想确保 303 列被转换为字符串而不是整数,以便我得到这个:
{'3030096843': 1, '3030096845': 1, -1: 2}
采纳答案by Andy Hayden
One way to convert to string is to use astype:
转换为字符串的一种方法是使用astype:
total_rows['ColumnID'] = total_rows['ColumnID'].astype(str)
However, perhaps you are looking for the to_jsonfunction, which will convert keys to valid json (and therefore your keys to strings):
但是,也许您正在寻找该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"}'
Note: you can pass in a buffer/file to save this to, along with some other options...
注意:您可以传入一个缓冲区/文件来保存它,以及其他一些选项......
回答by Surya
Here's the other one, particularly useful toconvert the multiple columns to stringinstead of just single column:
这是另一个,对于将多列转换为字符串而不是单列特别有用:
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
回答by Mike
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)
回答by dbouz
Using .apply()with a lambdaconversion function also works in this case:
使用.apply()具有lambda转换功能也能在这种情况下:
total_rows['ColumnID'] = total_rows['ColumnID'].apply(lambda x: str(x))
total_rows['ColumnID'] = total_rows['ColumnID'].apply(lambda x: str(x))
For entire dataframes you can use .applymap().
(but in any case probably .astype()is faster)
对于整个数据帧,您可以使用.applymap(). (但无论如何可能.astype()更快)
回答by Kranthi Kumar Valaboju
Use .astype(str)
使用.astype(str)
Ex:
前任:
Let d be the Pandas DataFrame
让 d 成为 Pandas DataFrame
d['Column_name'].astype(str)
d['Column_name'].astype(str)

