Pandas: change a certain row in percentage

I have a line in the Pandas data frame that contains the sales rate of my products.

Look at my details:

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

I can easily calculate% sales by doing the following:

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

Now what I'm trying to do is change the "Total Sales" line to the percentage of all numbers . I can do this if it is a column, but I ran into problems when working with rows.

Here is what I tried:

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

Calculations are shifted / shifted to the left. The sales rate provided for the 1st month is actually an indicator of sales for the 2nd month (39%)!

+4
source share
2 answers

.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 % , 100 ( 'f'), .


, Pandas DataFrame dtype. dtype object. , int64 int32 Total Listings Total Sales Python ints. Pandas NumPy NumPy (, int64 float64 - not object).

, , , DataFrame. , , .

, , 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. Pandas 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)
+6
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
0

All Articles