Explore Courses Blog Tutorials Interview Questions
0 votes
in Data Science by (50.2k points)

I have a dataframe in pandas with mixed int and str data columns. I want to concatenate first the columns within the dataframe. To do that I have to convert an int column to str. I've tried to do as follows:

mtrx['X.3'] = mtrx.to_string(columns = ['X.3'])


mtrx['X.3'] = mtrx['X.3'].astype(str)

but in both cases, it's not working and I'm getting an error saying "cannot concatenate 'str' and 'int' objects". Concatenating two str columns is working perfectly fine.

1 Answer

0 votes
by (108k points)

 Just change the data type of DataFrame column:

To int:

df.column_name = df.column_name.astype(np.int64)

To str:

df.column_name = df.column_name.astype(str)

If you are interested to learn Pandas visit this Python Pandas Tutorial.


Related questions

0 votes
1 answer
0 votes
1 answer
+2 votes
2 answers
asked May 25, 2019 in Python by Shubham (3.9k points)
0 votes
1 answer

Browse Categories