[英]Python Pandas DataFrame User Defined Function Transformations
我有幾個 DataFrame,我正在清理數據。 以下代碼獨立工作(在函數之外),但是,我必須將其應用於許多 DataFrame,並希望通過用戶定義的 function 清理此過程。 您能否幫助解決以下問題,以便它可用於我的所有數據框。
def format_df(df):
df.columns = df.columns.str
df.dropna(thresh=1, axis='columns',inplace = True)
df.dropna(thresh=80,axis=0,inplace = True)
df.columns = df.iloc[0]
df = df.iloc[1:].reset_index(drop=True)
df.columns = df.columns.str.replace(' ','',regex=False)
df.columns = df.columns.str.replace('($)','',regex=False)
df.columns = df.columns.str.replace('(Y/N)','Flag',regex=False)
df.columns = df.columns.str.replace('(x)','',regex=False)
df.columns = df.columns.str.replace('-','',regex=False)
return df
df.columns = df.columns.str
行不會運行,因為 df.columns.str 是一個字符串方法,而 df.columns 是一個索引。 相反,您可以使用astype方法:
df.columns = df.columns.astype(str)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.