Mayeul sgc
Mayeul sgc

Reputation: 2089

Dataframe.to_string() issue

I have a dataframe with text value and int inside. I apply df.to_string() without a problem, then I call a the get_value() function on it and get rid of the possible space at the beginning and at the end:

df.get_value(index,column).rstrip(' ').lstrip(' ')

This works fine, however if the value type is int, i got this error :

AttributeError: 'numpy.int64' object has no attribute 'rstrip'

Why does df.to_string() not work ?

Upvotes: 1

Views: 7371

Answers (1)

jezrael
jezrael

Reputation: 863511

It seems you need convert DataFrame to string by astype, for remove first and last whitespaces is better strip():

df = pd.DataFrame({'A':['  s  ','s','d'],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df)
       A  B  C  D  E  F
0    s    4  7  1  5  7
1      s  5  8  3  3  4
2      d  6  9  5  6  3

print (df.astype(str).get_value(0,'A').strip())
s

print (df.astype(str).loc[0,'A'].strip())
s

Upvotes: 3

Related Questions