Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error while concatenating values from a dataframe in python

I am reading a csv file using python and displaying the record after asking the user to select an index and then I display all the records corresponding to the index number.

Code:

df = pd.read_csv(csv_filepath, usecols=['userID', 'firstName', 'lastName'])
select_index = int(input("Enter the Index of Claimant: "))
data = df.loc[select_index, :]
print(data)

userID = data.userID
firstName = data.firstName
lastName = data.lastName
print(claimantID)
print(Claimant_FirstName)
print(Claimant_LastName)

The above code works perfectly fine and I am able to print the values.
Now I want to create a variable called as x and store userID + firstName + lastName .

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If I do this I am getting error. What can be the issue.
Basically if I write

x = userID + firstName + lastName

I get the following error:

numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('int64'), dtype('<U4')) -> None

>Solution :

The problem is with coloumn types.

Example:

import pandas as pd
df = pd.DataFrame({'userID': [1], 'firstName': ['a'], 'lastName':['b']})
data = df.loc[0, :]
userID = data.userID
firstName = data.firstName
lastName = data.lastName
#solution

x = str(userID) + firstName + lastName

result:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading