【问题标题】:Make me more smart让我更聪明
【发布时间】:2022-12-01 08:30:30
【问题描述】:
我想了解更多。
使用此代码 sn-p。
如何重写才能更有效?
df['collum1'] = df['collum1'].astype('str').str.replace(r".", r"", regex=False)
df['collum2'] = df['collum2'].astype('str').str.replace(r".", r"", regex=False)
df['collum3'] = df['collum3'].astype('str').str.replace(r".", r"", regex=False)
更干净,更有效
【问题讨论】:
标签:
python
pandas
dataframe
【解决方案1】:
我会尽力简化它,让它使用更少的内存?
df = {'col1':'hi','col2':'hi','col3':'hi'}
df['col1'] = str(df['col1']).replace(".","")
df['col2'] = str(df['col2']).replace(".","")
df['col3'] = str(df['col3']).replace(".","")
这样做的目的是简化它以使用 Python 提供的更简单的代码,有时使其更快。它首先做的是创建一个字典,然后将每个值转换为字符串并修改它们以删除字符串中的所有.s。