pandas 将数字转换为熊猫数据框中的 2 位浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49314624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:20:14  来源:igfitidea点击:

Converting numbers to 2 digits float number in pandas dataframe

pythonpandasdataframe

提问by Meruemu

I have a pandas dataframe as follow:

我有一个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

I'd like to all float numbers to be '.2f' (2 digits float) numbers. .applymap()does not work since I have string type in "Names" column. Is there a work around using .applymap()or is there a better way to do this?

我希望所有浮点数都是“.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)

updated with my current solution as above.

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

回答by jezrael

Use:

用:



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

EDIT:

编辑:

I try improve your solution:

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

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