2017-03-02 7 views
1

私はテキスト値とintを持つデータフレームを持っています。 私はその後、私はそれにget_value()関数を呼び出すと、最初と最後で可能スペースを取り除くため、問題なくdf.to_string()を適用します。Dataframe.to_string()issue

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

これは、値の型がint型であるがあれば、正常に動作します私はこのエラーを受け取りました:

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

df.to_string()はなぜ機能しませんか?あなたのための最初と最後の空白を削除し、astypeによりstringDataFrameを変換する必要があるようだ

+0

- 'DF = DF .astype(str) ' – jezrael

+0

これはおそらくこのものと重複しています。 http://stackoverflow.com/a/17950531/5226708 – Levi

答えて

3

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 
あなたが最初DataFrame` `のすべての値をSTRING``に変換することができ