简体   繁体   English

如何在熊猫数据框值中添加“ $”并使用列作为索引?

[英]How to add '$' to my pandas dataframe values and use a column as index?

I have a table as follows: 我有一张桌子,如下所示:

           Names  Cider  Juice  Subtotal(Cider)  Subtotal(Juice)  Total
0        Richard   13.0    9.0            71.50            40.50  112.0
0         George    7.0   21.0            38.50            94.50  133.0
0           Paul    0.0   23.0             0.00           103.50  103.5
0           John   22.0    5.0           121.00            22.50  143.5
Total        sum   42.0   58.0           231.00           261.00  492.0
Average      avg   10.5   14.5            57.75            65.25  123.0

Values in [Subtotal(Cider) Subtotal(Juice) Total] are user input of float type. [Subtotal(Cider) Subtotal(Juice) Total]是浮动类型的用户输入。

How can I add a '$' to the values of these columns and use the Names column as my table index? 如何在这些列的值中添加“ $”并将“ Names列用作表索引? I want a final table like this: 我想要这样的决赛桌:

Names   Cider   Juice   Subtotal (Cider)   Subtotal (Juice) Total

Richard   13        9           $ 71.50            $ 40.50 $ 112.00
George     7       21           $ 38.50            $ 94.50 $ 133.00
Paul       0       23           $ 0.00            $ 103.50 $ 103.50
John      22        5           $ 121.00           $ 22.50 $ 143.50
Total     42       58           $ 231.00          $ 261.00 $ 492.00
Average 10.50   14.50           $ 57.75            $ 65.25 $ 123.00

My code runs like this: 我的代码是这样运行的:

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 #" + str(i + 1) + " ")  # type str

    cider_orderred = float(input("How many orders of cider did {} have? ".format(names)))  # type str
    juice_orderred = float(input("How many orders of juice did {} have? ".format(names)))  # type str

    # store the values of the subtotals from user inputs
    cider_sub = 5.50 * cider_orderred  # type float
    juice_sub = 4.50 * juice_orderred  # type float
    total = cider_sub + juice_sub  # type float

    # create the 4x6 table
    df1 = pd.DataFrame(
        data=[[names, cider_orderred, juice_orderred, cider_sub, juice_sub, total]],
        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()

# Set the row name to "Total" and "Average"
df.iloc[int(people_ordered),0] = 'Total'
df.iloc[int(people_ordered)+1,0] = 'Average'

# Adding "$" to the prices


df.index = range(len(df.index))
# Set the index according to 'Names'
df.set_index('Names')

print(df)

To add a string, in this case'$', to the front of each value in the specified columns you can do the following, 要将字符串(在这种情况下为“ $”)添加到指定列中每个值的前面,您可以执行以下操作:

df['Subtotal(Cider)'] = '$' + df['Subtotal(Cider)'].astype(str)
df['Subtotal(Juice)'] = '$' + df['Subtotal(Juice)'].astype(str)
df['Total'] = '$' + df['Total'].astype(str)

For the second question, to set the Names column as index simply use 对于第二个问题,只需将Names列设置为索引即可

df.set_index('Names', inplace=True)

Note that this will change the names of the Total and Average columns that you set. 请注意,这将更改您设置的“ Total和“ Average列的名称。 A simple solution would be to add those two afterwards. 一个简单的解决方案是在其后添加这两个。

Dataframes have a method to_string that accept column specific formatting functions 数据框具有to_string方法,可以接受列特定的格式设置功能

  1. set the index using set_index , but first fix the index for the last two values of df.Names 使用set_index设置索引,但首先为df.Names的最后两个值修复索引。

     df['Names'].iloc[-2:] = df.index[-2:] df.set_index('Names', inplace=True) 
  2. create the output string using the to_string & formatters 使用to_string和格式化程序创建输出字符串

     cols = ['Subtotal(Cider)', 'Subtotal(Juice)', 'Total'] def f(x): return '$ {0:0.2f}'.format(x) outstr = df.to_string(formatters={k: f for k in cols}) print(outstr) # outputs: Cider Juice Subtotal(Cider) Subtotal(Juice) Total Names Richard 13.0 9.0 $ 71.50 $ 40.50 $ 112.00 George 7.0 21.0 $ 38.50 $ 94.50 $ 133.00 Paul 0.0 23.0 $ 0.00 $ 103.50 $ 103.50 John 22.0 5.0 $ 121.00 $ 22.50 $ 143.50 Total 42.0 58.0 $ 231.00 $ 261.00 $ 492.00 Average 10.5 14.5 $ 57.75 $ 65.25 $ 123.00 
  3. if working in a jupyter notebook, you should use dataframe styling , which similarly allows passing of individual column formatting options. 如果在jupyter笔记本上工作,则应使用dataframe styling ,它类似地允许传递各个列格式选项。 Note that this won't style your dataframe when displayed in the console. 请注意,当在控制台中显示时,这不会为您的数据框设置样式。

    example: 例:

     df.style.format({k: f for k in cols}) 

Doing it via formatting functions has the following benefits: 通过格式化功能执行此操作具有以下好处:

  • you retain the original data types, only the output string is formatted, so you can continue to use your dataframe for more analysis. 您保留原始数据类型,仅格式化输出字符串,因此您可以继续使用数据框进行更多分析。
  • you have very granular control on how each field is formatted. 您可以非常精细地控制每个字段的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM