2017-03-14 7 views
2
df = pd.DataFrame({'A' : ['one', 'two three', 'f four ', 'three '], 'B': [10,20,30,40]}) 

df = df.set_index('A') 

に末尾の空白を排除することができますが、DFがする必要があるものです:Pandas-はどのように私はインデックスここ

  B 
A 
one  10 
two three 20 
f four  30 
three  40 

最後のデータフレームでは、彼らは末尾されている場合、インデックスにスペースを削除する必要があります。彼らがどこにいても残っていれば、彼らは残る必要があります。したがって、 'three'と 'f four'の末尾の空白を削除する必要があります。

答えて

2

私はあなたがstripが必要だと思う:

print (df.index.tolist()) 
['one', 'two three', 'f four ', 'three '] 

df.index = df.index.str.strip() 
print (df) 
      B 
A    
one  10 
two three 20 
f four  30 
three  40 

print (df.index.tolist()) 
['one', 'two three', 'f four', 'three'] 
関連する問題