python - 将数字转换为 Pandas 数据框中的2位 float

标签 python pandas 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

我希望所有 float 都是“.2f”(2 位 float )数字。 .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)

如上更新了我当前的解决方案。

最佳答案

使用:


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

编辑:

我尝试改进您的解决方案:

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

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

关于python - 将数字转换为 Pandas 数据框中的2位 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49314624/

相关文章:

python - Pandas - 两列作为索引(有效时间和行号)?

python - 根据其他列上的某些条件更新 dask 数据框中列的值

python - 使用纪元时间格式的给定时间之间的随机时间

python - Python 字典键中的空格

python - 如何从外部使用 python 脚本在 mininet 提示符内运行命令

python - 数据框中的多个拼写结果

python - 从 pandas 数据帧内的 nltk.stanford.StanfordDependencyParser 中解压 list_iterator

python - 连接使用循环生成的 pandas DataFrame

python - 如何按日期将三个 pandas 系列合并到一个数据框中?

python - 这段代码有什么问题?