0

I have some columns ['subject', 'H.period', 'DD.period.t'] etc. Actually all columns are object type.

enter image description here

dtype printscreen

How can i convert these columns into string type?

And how can i use .replace for converting "," into "." in a csv file? I need to use these data in machine learning K Neighbors algorithm.

bigbounty
  • 16,526
  • 5
  • 37
  • 65
Alex Colombari
  • 159
  • 1
  • 16

2 Answers2

4

There is no string dtype in pandas. As noted in the docs:

Note When working with heterogeneous data, the dtype of the resulting ndarray will be chosen to accommodate all of the data involved. For example, if strings are involved, the result will be of object dtype. If there are only floats and integers, the resulting array will be of float dtype.

As far as replacing , for . in your whole dataframe, Use replace with regex = True:

df = df.replace(',','.',regex=True)
# or
df.replace(',','.',regex=True, inplace = True)

For example: If your dataframe df looks like:

>>> df
  col1         col2
0  x,x    blah,blah
1  y,z  hello,world
2  z.z       ,.,.,.

Then:

df = df.replace(',','.',regex=True)
>>> df
  col1         col2
0  x.x    blah.blah
1  y.z  hello.world
2  z.z       ......
sacuL
  • 49,704
  • 8
  • 81
  • 106
0

Although the dtype is indeed 'object', when applying the type() function to the columns labels individually you will find out that they do actually belong to the class 'str'. So that is fine.

As to your question about replacement I would suggest something like this:

length = len(df[df.columns[0]])
for column in df.columns:
     for index in range(length):
          df[column][index] = df[column][index].replace(",",".")