.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)