我有一张表格如下:
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
[Subtotal(Cider) Subtotal(Juice) Total]
中的值是浮点类型的用户输入。
如何添加' $'到这些列的值并使用Names
列作为我的表索引?我想要一个像这样的决赛桌:
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
我的代码运行如下:
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)
答案 0 :(得分:1)
要将字符串(在本例中为“$”)添加到指定列中每个值的前面,您可以执行以下操作:
df['Subtotal(Cider)'] = '$' + df['Subtotal(Cider)'].astype(str)
df['Subtotal(Juice)'] = '$' + df['Subtotal(Juice)'].astype(str)
df['Total'] = '$' + df['Total'].astype(str)
对于第二个问题,要将Names列设置为索引,只需使用
df.set_index('Names', inplace=True)
请注意,这会更改您设置的Total
和Average
列的名称。一个简单的解决方案是在之后添加这两个。
答案 1 :(得分:1)
数据框有一个方法to_string
,可以接受列特定的格式化函数
使用set_index
设置索引,但首先修复df.Names
的最后两个值的索引
df['Names'].iloc[-2:] = df.index[-2:]
df.set_index('Names', inplace=True)
使用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
如果在jupyter笔记本中工作,则应使用dataframe styling,同样允许传递单个列格式选项。请注意,在控制台中显示时,这不会为您的数据框设置样式。
示例:
df.style.format({k: f for k in cols})
通过格式化功能实现它有以下好处: