2016-06-29 123 views
4

我在Pandas数据框中有一行,其中包含我的项目的销售率。熊猫:将特定的行更改为百分比

看看我的数据:

block_combine 
Out[78]: 
END_MONTH   1 2 3 4 5 
Total Listings 168 219 185 89 112 
Total Sales  85 85 84 41 46 

我可以很容易地通过做计算销售额%以下:

block_combine.loc["Total Sales Rate"] = block_combine.ix[1,:]/block_combine.ix[0,:] 
block_combine 

Out[79]: 
END_MONTH     1   2   3   4   5 
Total Listings 168.000000 219.000000 185.000000 89.000000 112.000000 
Total Sales  85.000000 85.000000 84.000000 41.000000 46.000000 
Total Sales Rate 0.505952 0.388128 0.454054 0.460674 0.410714 

现在什么,我试图做的是改变“销售总额率“排到整数百分比。如果它是一个列,我可以做到这一点,但是当我使用行时遇到问题。

以下是我尝试:

block_combine.loc["Total Sales Rate"] = pd.Series(["{0:.0f}%".format(val * 100) for val in block_combine.loc["Total Sales Rate"]]) 


block_combine 

Out[81]: In [82]: 
END_MONTH   1 2 3 4  5 
Total Listings 168 219 185 89 112.0 
Total Sales  85 85 84 41 46.0 
Total Sales Rate 39% 45% 46% 41% NaN 

的计算是关闭/向左偏移。第1个月的销售率实际上是第2个月的销售率(39%)!

回答

6

你可以使用.apply('{:.0%}'.format)

import pandas as pd 

df = pd.DataFrame([(168,219,185,89,112), (85,85,84,41,46)], 
        index=['Total Listings', 'Total Sales'], columns=list(range(1,6))) 
df.loc['Total Sales Rate'] = ((df.loc['Total Sales']/df.loc['Total Listings']) 
           .apply('{:.0%}'.format)) 

print(df) 

产量

    1 2 3 4 5 
Total Listings 168 219 185 89 112 
Total Sales  85 85 84 41 46 
Total Sales Rate 51% 39% 45% 46% 41% 

注意,Python的str.format方法具有built-in % format其在固定( 'F')格式100和显示相乘的数量,接着是百分号。


请务必注意,Pandas DataFrame列必须具有单个dtype。将一个值更改为字符串会强制整列将其dtype更改为object dtype。因此,行中的int64s或int32s中的 Total ListingsTotal Sales行被重新渲染为普通的Python ints。这个 可以防止Pandas利用基于NumPy的快速数值运算 ,这些操作仅适用于本地NumPy dtypes(如int64float64 - 不是 object)。

因此,虽然上面的代码实现了所需的外观,但如果在DataFrame上进行进一步的计算,则不建议使用 。相反,如果您需要为演示文稿执行操作,则最终只能将 转换为字符串。

,或者,转你的数据帧,因此Total Sales Rate字符串都在列​​,而不是一行:

import pandas as pd 

df = pd.DataFrame([(168,219,185,89,112), (85,85,84,41,46)], 
        index=['Total Listings', 'Total Sales'], columns=list(range(1,6))).T 

df['Total Sales Rate'] = ((df['Total Sales']/df['Total Listings']) 
           .apply('{:.0%}'.format)) 

print(df) 

产生

Total Listings Total Sales Total Sales Rate 
1    168   85    51% 
2    219   85    39% 
3    185   84    45% 
4    89   41    46% 
5    112   46    41% 

之所以

block_combine.loc["Total Sales Rate"] = pd.Series(["{0:.0f}%".format(val * 100) for val in block_combine.loc["Total Sales Rate"]]) 

将值向左移动一列是因为新系列的索引从0开始而不是1开始。在给block_combine.loc["Total Sales Rate"]赋值之前,熊猫对齐右边的系列的索引,索引为block_combine.loc["Total Sales Rate"]

因此,你可以或者已经使用:

block_combine.loc["Total Sales Rate"] = pd.Series(["{0:.0f}%".format(val * 100) 
    for val in block_combine.loc["Total Sales Rate"]], 
    index=block_combine.columns) 
0
df = pd.DataFrame({ 
     1: [168,85], 
     2: [219,85], 
     3: [185,84], 
     4: [89,41], 
     5: [112,46] 
    }, index=['Total Listings', 'Total Sales']) 

total_sales_rate = pd.Series(df.loc['Total Sales']/df.loc['Total Listings'] * 100, name='Total Sales Rate').round() 
df = df.append(total_sales_rate) 

结果...

    1 2 3 4 5 
Total Listings 168 219 185 89 112 
Total Sales  85 85 84 41 46 
Total Sales Rate 51 39 45 46 41 
相关问题