0
我正在讀取一個excel文件到DataFrame中。我需要從所有單元格中刪除空格,而其他單元格在Python 3.5中保持不變。 例如:如何從Python DataFrame中去除空格在這個例子中
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
#read data from DataFrame
data_ThisYear_Period=[[' 序 號','北 京','上 海',' 廣州'],\
[' 總計','11232',' 2334','3 4'],\
[' 溫度','1223','23 23','2323'],\
['人 口','1232','21 321','1222'],\
['自行車', '1232', '21321', '12 22']]
data_LastYear_Period=DataFrame(data_ThisYear_Period)
print(type(data_LastYear_Period))
data_ThisYear_Period.apply(data_ThisYear_Period.str.strip(),axis=1)
回溯(最近通話最後一個): 文件 「C:/test/temp.py」 17行,在 data_ThisYear_Period.apply(data_ThisYear_Period.str.strip() ,軸= 1) AttributeError的:「列表」對象沒有屬性「應用」
如何從數據幀的Python剝離空格在這個例子中
非常感謝你!在這個例子中,它可以工作,但是當我從excel中讀取DataFrame時,它會失效:AttributeError:(「'int'object has no attribute'split'」,'index on sequence sequence number'),我該如何解決它?謝謝 ! – chenhong
如果有一個int,那麼你可以在lambda函數中使用if else。請參閱編輯。 – plasmon360
謝謝!有用! – chenhong