如何优化此替换列和索引的代码?
Posted
技术标签:
【中文标题】如何优化此替换列和索引的代码?【英文标题】:How can I optimize this code that does replacement of columns and indexing? 【发布时间】:2017-12-05 11:42:33 【问题描述】:census_subdivision_profile_merged
是一个数据框,我在这里分别做很多事情。有没有办法一次完成所有这些?
# Drop missing data
census_subdivision_profile_merged = census_subdivision_profile_merged.dropna()
census_subdivision_profile_merged.columns = [census_subdivision_profile_merged.columns[i]+' '+str(i) for i in range(len(census_subdivision_profile_merged.columns))]
census_subdivision_profile_merged.columns = [census_subdivision_profile_merged.columns[i].replace(" ", "_") for i in range(len(census_subdivision_profile_merged.columns))]
census_subdivision_profile_merged.columns = [census_subdivision_profile_merged.columns[i].replace(",", "_") for i in range(len(census_subdivision_profile_merged.columns))]
census_subdivision_profile_merged.columns = [census_subdivision_profile_merged.columns[i].replace("-", "_") for i in range(len(census_subdivision_profile_merged.columns))]
census_subdivision_profile_merged.columns = [census_subdivision_profile_merged.columns[i].replace("%", "_") for i in range(len(census_subdivision_profile_merged.columns))]
census_subdivision_profile_merged.columns = [census_subdivision_profile_merged.columns[i].replace("$", "_") for i in range(len(census_subdivision_profile_merged.columns))]
【问题讨论】:
您是否只清理列标题中的文本?你能发布一个小的示例数据框吗? 【参考方案1】:您使用字符串执行 5 次替换方法,但您也可以使用正则表达式,例如:
import re
#Test data frame
df=pd.DataFrame("data1":["E %,-$p,e","E $m$$-%ple"],"data2":["E %,-$p,e","E $m$$-%ple"])
#Remove all special characters and whitespaces for each row, for each word
for j in df.columns:
for strs in range(len(df[j])):
df.loc[strs,j]=re.sub(r'[-%,$\s]',"_",df.loc[strs,j])
print(df)
对于您的示例应该可以:
for j in census_subdivision_profile_merged.columns:
for strs in range(len(census_subdivision_profile_merged[j])):
census_subdivision_profile_merged.loc[strs, j] = re.sub(r'[-%,$\s]', "_", census_subdivision_profile_merged.loc[strs, j])
或者,您可以尝试从完整的 DataFrame 中删除所有特殊字符和空格。
census_subdivision_profile_merged = census_subdivision_profile_merged.replace(r"[-%,$\s]","_",regex=True)
【讨论】:
以上是关于如何优化此替换列和索引的代码?的主要内容,如果未能解决你的问题,请参考以下文章