Mer*_*emu 2 python dataframe pandas
我有一个熊猫数据框,如下所示:
Names Cider Juice Subtotal (Cider) Subtotal (Juice) Total
Richard 13 9 $ 71.5 $ 40.5 $ 112.0
George 7 21 $ 38.5 $ 94.5 $ 133.0
Paul 0 23 $ 0.0 $ 103.5 $ 103.5
John 22 5 $ 121.0 $ 22.5 $ 143.5
Total 42 58 $ 231.0 $ 261.0 $ 492.0
Average 10.5 14.5 $ 57.75 $ 65.25 $ 123.0
Run Code Online (Sandbox Code Playgroud)
我希望所有浮点数均为'.2f'(2位浮点数)数字。.applymap()不起作用,因为我在“名称”列中输入了字符串类型。是否有围绕使用的解决方法,.applymap()或者有更好的方法来做到这一点?
import pandas as pd
df = pd.DataFrame(columns=["Names", "Cider", "Juice", "Subtotal(Cider)", "Subtotal(Juice)", "Total"])
people_ordered = input('How many people ordered? ') # type str
# Create the 4x3 table from user input
for i in range(int(people_ordered)):
names = input("Enter the name of Person #{}: ".format(i+1)) # type str
cider_orderred = float(input("How many orders of cider did {} have? ".format(names))) # type str -> int
#cider_orderred = float("{:.2f}".format(cider_orderred))
juice_orderred = float(input("How many orders of juice did {} have? ".format(names))) # type str -> int
#juice_orderred = float("{:.2f}".format(juice_orderred))
# store the values of the subtotals from user inputs
cider_sub = 5.50 * cider_orderred # type float
cider_sub = float("{:.2f}".format(cider_sub))
juice_sub = 4.50 * juice_orderred # type float
juice_sub = float("{:.2f}".format(juice_sub))
total = cider_sub + juice_sub # type float
total = float("{:.2f}".format(total))
# create the 4x6 table
df1 = pd.DataFrame(
data=[[names, int(cider_orderred), int(juice_orderred), round(cider_sub, 2), round(juice_sub, 2), round(total, 2)]],
columns=["Names", "Cider", "Juice", "Subtotal(Cider)", "Subtotal(Juice)", "Total"])
# merge the the 4x3 into the 4x6 table
df = pd.concat([df, df1], axis=0)
# add rows of "Total" and "Average"
df.loc['Total'] = df.sum()
df.loc['Average'] = df[:int(people_ordered)].mean()
# Adding "$" to the prices
df['Subtotal(Cider)'] = '$ ' + df['Subtotal(Cider)'].astype(str)
df['Subtotal(Juice)'] = '$ ' + df['Subtotal(Juice)'].astype(str)
df['Total'] = '$ ' + df['Total'].astype(str)
# Set the row name to "Total" and "Average"
df.iloc[int(people_ordered),0] = 'Total'
df.iloc[int(people_ordered)+1,0] = 'Average'
# Set the index according to 'Names'
df.index = range(len(df.index))
df.set_index('Names', inplace=True)
print(df)
Run Code Online (Sandbox Code Playgroud)
如上所述更新了我当前的解决方案。
使用:
df = (df.set_index('Names')
.replace('\$\s+','', regex=True)
.astype(float)
.applymap('{:,.2f}'.format))
print (df)
Cider Juice Subtotal (Cider) Subtotal (Juice) Total
Names
Richard 13.00 9.00 71.50 40.50 112.00
George 7.00 21.00 38.50 94.50 133.00
Paul 0.00 23.00 0.00 103.50 103.50
John 22.00 5.00 121.00 22.50 143.50
Total 42.00 58.00 231.00 261.00 492.00
Average 10.50 14.50 57.75 65.25 123.00
Run Code Online (Sandbox Code Playgroud)
编辑:
我尝试改善您的解决方案:
people_ordered = input('How many people ordered? ')
Data = []
# Create the 4x3 table from user input
for i in range(int(people_ordered)):
names = input("Enter the name of Person #{}: ".format(i+1)) # type str
cider_orderred = int(input("How many orders of cider did {} have? ".format(names))) # type str -> int
juice_orderred = int(input("How many orders of juice did {} have? ".format(names))) # type str -> int
#create in loop tuple and append to list Data
Data.append((names, cider_orderred, juice_orderred))
#create DataFrame form list of tuples, create index by Names
df1 = pd.DataFrame(Data, columns=['Names','Cider','Juice']).set_index('Names')
#count all new columns, rows
df1['Subtotal(Cider)'] = df1['Cider'] * 5.5
df1['Subtotal(Juice)'] = df1['Juice'] * 4.5
df1['Total'] = df1['Subtotal(Cider)'] + df1['Subtotal(Juice)']
df1.loc['Total'] = df1.sum()
#remove row Total for correct mean
df1.loc['Average'] = df1.drop('Total').mean()
#get custom format of columns in list cols
cols = ['Subtotal(Cider)','Subtotal(Juice)','Total']
df1[cols] = df1[cols].applymap('$ {:,.2f}'.format)
#create column from index
df1 = df1.reset_index()
Run Code Online (Sandbox Code Playgroud)
print(df1)
Names Cider Juice Subtotal(Cider) Subtotal(Juice) Total
0 r 13.0 9.0 $ 71.50 $ 40.50 $ 112.00
1 g 7.0 21.0 $ 38.50 $ 94.50 $ 133.00
2 p 0.0 23.0 $ 0.00 $ 103.50 $ 103.50
3 j 22.0 5.0 $ 121.00 $ 22.50 $ 143.50
4 Total 42.0 58.0 $ 231.00 $ 261.00 $ 492.00
5 Average 10.5 14.5 $ 57.75 $ 65.25 $ 123.00
Run Code Online (Sandbox Code Playgroud)
一般情况下将所有浮点设置为 2 位即可
pd.options.display.float_format = "{:.2f}".format
Run Code Online (Sandbox Code Playgroud)
虽然: df['column'].sum() 不会变成 2 位数字...?
| 归档时间: |
|
| 查看次数: |
4352 次 |
| 最近记录: |