【问题标题】:Transform a dataframe with comma thousand separator to space separator Pandas将带有逗号千位分隔符的数据框转换为空格分隔符 Pandas
【发布时间】:2021-08-11 10:56:38
【问题描述】:

我对 Pandas 的格式有疑问。因此,我在 A DataFrame 中有一个 Column,它使用逗号分隔符(如(200,000))计算数字。所以我想把它转换成(200 000)。

使用替换功能的简单方法,但我也想将类型转换为整数。它不起作用,因为它们之间有空格。

最后,我只想用降序排列的值进行排名,如下所示:

Id Villas Price_nospace
3 Peace 35000000
3 Peace 35000000
2 Rosa 27000000
1 Beach 25000000
0 Palm 22000000

如您所见,没有分隔符很难阅读价格。所以我想让价格更具可读性。但是当我有空格分隔符时,我无法转换为 Int。 如果我不转换为整数,我可以使用 sort_values 函数。所以我被卡住了。

感谢您的帮助。

【问题讨论】:

  • df['Price_nospace']=df['Price_nospace'].str.replace(',','',regex=True).astype(int)

标签: pandas dataframe type-conversion integer separator


【解决方案1】:

另一种选择是改变数据的显示方式,但不影响基础类型。

在将str 价格转换为float 价格后使用pd.options.display.float_format

import pandas as pd


def my_float_format(x):
    '''
    Number formatting with custom thousands separator
    '''
    return f'{x:,.0f}'.replace(',', ' ')


# set display float_format
pd.options.display.float_format = my_float_format

df = pd.DataFrame({
    'Id': [3, 3, 2, 1, 0],
    'Villas': ['Peace', 'Peace', 'Rosa', 'Beach', 'Palm'],
    'Price_nospace': ['35,000,000', '35,000,000', '27,000,000',
                      '25,000,000', '22,000,000']
})

# Convert str prices to float
df['Price_nospace'] = (
    df['Price_nospace'].str.replace(',', '', regex=True).astype(float)
)

Output:

print(df)

   Id Villas  Price_nospace
0   3  Peace     35 000 000
1   3  Peace     35 000 000
2   2   Rosa     27 000 000
3   1  Beach     25 000 000
4   0   Palm     22 000 000
print(df.dtypes)

Id                 int64
Villas            object
Price_nospace    float64
dtype: object

由于类型为float64,任何数字运算都将正常运行。


同样的my_float_format 函数也可以用于导出:

df.to_csv(float_format=my_float_format)
,Id,Villas,Price_nospace
0,3,Peace,35 000 000
1,3,Peace,35 000 000
2,2,Rosa,27 000 000
3,1,Beach,25 000 000
4,0,Palm,22 000 000

【讨论】:

    【解决方案2】:

    稍微修改了样本输入以对输出中的值进行排序(降序)。

    以下解决方案将按Price_nospace 对数据帧进行排序(降序),并将comma 替换为space。但Price_nospace 将是object 类型的输出。

    示例输入

    Id  Villas  Price_nospace
    0   3   Peace   220,000
    1   3   Peace   350,000
    2   2   Rosa    270,000
    3   1   Beach   250,000
    4   0   Palm    230,000
    

    代码

    df['Price_new'] = df['Price_nospace'].str.replace(',','',regex=True).astype(int)
    df = df.sort_values(by='Price_new', ascending=False)
    df['Price_nospace'] = df['Price_nospace'].str.replace(',',' ',regex=True)
    df = df.drop(columns='Price_new').reset_index(drop=True) ## reset_index, if required
    df
    

    输出

        Id  Villas  Price_nospace
    0   3   Peace   350 000
    1   2   Rosa    270 000
    2   1   Beach   250 000
    3   0   Palm    230 000
    4   3   Peace   220 000
    

    说明

    引入了一个新列 Price_new 以将 Price_nospace 值转换为 int 并对值进行排序。
    对 df 进行排序后,只需替换 comma with space for Price_nospace 并删除 temp 列 Price_new

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      相关资源
      最近更新 更多