1

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 ?

2
  • You can convert to string all values of DataFrame first - df = df.astype(str) Commented Mar 2, 2017 at 7:37
  • Maybe duplicated with this one. stackoverflow.com/a/17950531/5226708 Commented Mar 2, 2017 at 7:42

1 Answer 1

3

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