简体   繁体   English

从熊猫数据框中检索值时将整数转换为字符串类型

[英]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.

相关问题 将熊猫数据框中的字符串转换为整数 - Convert string in pandas dataframe to integer 如何在 pandas dataframe 中将多列从字符串转换为 integer? - How to convert multiple columns from string to integer in pandas dataframe? 如何将最后几列从pandas中的字符串类型转换为整数 - How to convert the last several columns to integer from string type in pandas 如何将浮点数列表转换为 integer。 当那里有 NaN 值时! 我从 pandas dataframe 得到这个列表,其中有一些 null 值 - how to convert a list of floats into integer. When there are NaN values in there! I got this lists from pandas dataframe which have some null values 在 Pandas 数据框中将列类型从字符串转换为日期时间格式 - Convert the column type from string to datetime format in Pandas dataframe 如何在 pandas.DataFrame 中将“对象/字符串”类型转换为“时间”类型? - How to convert from 'object/string' to 'time' type in pandas.DataFrame? 将包含NaN值的整个Pandas数据帧从string转换为float - Convert whole Pandas dataframe containing NaN values from string to float 从字符串转换为熊猫数据框 - convert from string to pandas dataframe 无法将Pandas数据框中的列转换为整数数据类型 - Failing to convert column in pandas dataframe to integer data type 从字符串中删除字母并转换为 pandas 中的 integer - Remove letters from string and convert to integer in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM