1

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.

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)).

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)

So, I am guessing that the data type from the List generated by Pandas is an Integer.

So, how do I convert the data type from Integer to String?

Which line should I add something like str(10), for an example?

3
  • You want to convert ID to string? Commented Aug 24, 2017 at 15:27
  • ID is just the name of column. Commented Aug 24, 2017 at 16:03
  • Yes, I understood that. Commented Aug 24, 2017 at 16:03

1 Answer 1

2

You can use pd.Series.astype:

dgl = dg['ID'].astype(str).values.tolist()
print(dgl)

Output:

['111111', '2222', '3333333', '444444']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.