我有一个包含数百个帐号及其描述的数据集。它已从 Excel 导入 Python 数据框。Excel 中的描述具有不同数量的前导和尾随空格。Account number 是一个整数,Description 是一个对象,End Balance 是一个浮点数。
我尝试去除前导和尾随空格,用单个替换多个空格,但是当我使用 groupby 时,它不会将描述识别为相同。如果我只 groupby Account 我得到 435 行,这是正确的。如果我 groupby 描述我得到超过 1100 这是不正确的(这是原始的行数)。按帐户和描述分组产生与按描述分组相同的结果。这对我来说意味着描述仍然不被视为相同。
我也尝试过完全不脱衣服,然后毫无喜悦地离开。
关于如何使描述相同的任何想法?
# Replaces multiple white spaces in string to a single whitespace
PE5901_df['Description'] = PE5901_df['Description'].str.replace('\s+', ' ', regex=True)
# Strip leading and trailing spaces from fields to avoid groupby, concat, and merge issues later.
PE5901_df['Description'] = PE5901_df['Description'].str.strip()
# Groupby Account number and Asset name - sums individual rows with identical account numbers.
PE5901_df=PE5901_df.groupby(['Account','Description'],as_index=False).sum()