[英]Convert integer to string type when retrieving values from a pandas dataframe
I am trying to convert output of data (from Integer to String) from a List generated using Pandas. 我试图将使用熊猫生成的列表的数据输出(从整数转换为字符串)。
I got the output of data from a csv file. 我从csv文件中获得了数据输出。
Here is my code that covers expression using Pandas (excluding part where it shows how to come up with generation of object 'InFile' (csv file)). 这是我的代码,涵盖了使用熊猫表达的内容(不包括显示如何生成对象“ InFile”(csv文件)的部分)。
import pandas as pd
....
with open(InFile) as fp:
skip = next(it.ifilter(
lambda x: x[1].startswith('ID'),
enumerate(fp)
))[0]
dg = pd.read_csv(InFile, usercols=['ID'], skiprows=skip)
dgl = dg['ID'].values.tolist()
Currently, output is a List (example below). 当前,输出是一个列表(下面的示例)。
[111111, 2222, 3333333, 444444]
I am trying to match data from other List (which is populated into String or Varchar(data type in MySQL), but somehow, I cannot come up with any match. My previous post -> How to find match from two Lists (from MySQL and csv) 我正在尝试匹配其他列表中的数据(在MySQL中填充为String或Varchar(MySQL中的数据类型),但是以某种方式,我无法提出任何匹配项。我以前的文章-> 如何从两个列表中找到匹配项(来自MySQL和csv)
So, I am guessing that the data type from the List generated by Pandas is an Integer . 因此,我猜想Pandas生成的List中的数据类型是Integer 。
So, how do I convert the data type from Integer to String? 那么,如何将数据类型从Integer转换为String?
Which line should I add something like str(10) , for an example? 例如,我应该在哪一行添加str(10)之类的内容?
You can use pd.Series.astype
: 您可以使用pd.Series.astype
:
dgl = dg['ID'].astype(str).values.tolist()
print(dgl)
Output: 输出:
['111111', '2222', '3333333', '444444']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.