Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用Python打印语句以显示数据帧上数学运算的结果?_Python_Pandas_Slice - Fatal编程技术网

如何用Python打印语句以显示数据帧上数学运算的结果?

如何用Python打印语句以显示数据帧上数学运算的结果?,python,pandas,slice,Python,Pandas,Slice,所以我有一个简单的按性别划分的销售汇总数据框架: Gender | Sales ___________________ M | 25 F | 30 我现在只想用Python返回一行,内容是:销售金额的平均差额为16.67% 这就是30-25除以30,再乘以100;我想在末尾加个%号 我试过: m_sales = df.loc[df['Gender'] == 'M'] f_sales = df.loc[df['Gender'] == 'F'] print(

所以我有一个简单的按性别划分的销售汇总数据框架:

Gender |    Sales
___________________
M      |    25
F      |    30
我现在只想用Python返回一行,内容是:销售金额的平均差额为16.67%

这就是30-25除以30,再乘以100;我想在末尾加个%号

我试过:

m_sales = df.loc[df['Gender'] == 'M']
f_sales = df.loc[df['Gender'] == 'F']

print('The mean gap in the amount sold is:', m_sales['Sales'] - f_sales['Sales'] / m_sales['Sales'] * 100, '%')
不幸的是,这不起作用。我得到:

销售金额的平均差距为:0 1楠 名称:销售,数据类型:对象%

想一想好吗?我是一个非常初级的人,很抱歉这样一个基本的问题

[“Sales”].iloc[0]
附加到筛选表达式中,以直接获取
M
F
的值,然后将这些更改也投影到
print()
函数:

m_sales = df.loc[df['Gender'] == 'M']["Sales"].iloc[0]
f_sales = df.loc[df['Gender'] == 'F']["Sales"].iloc[0]

print('The mean gap in the amount sold is:', (f_sales - m_sales) / f_sales * 100, '%')

解释如下:

  • df.loc[df['Gender']='M']
    是一个数据帧
  • 通过添加
    [“Sales”]
    选择
    “Sales”
    列,您将获得一个系列(仅包含1个元素),并且
  • 通过附加
    .iloc[0]
    可以获得该系列的第一个(=唯一的一个)元素

注:

您可以使用f-string(用于Python 3.6+)或
.format()
方法来调整输出,例如

print(f'The mean gap in the amount sold is: {(f_sales - m_sales) / f_sales * 100:.2f}%')
[“Sales”].iloc[0]
附加到筛选表达式中,以直接获取
M
F
的值,然后将这些更改也投影到
print()
函数:

m_sales = df.loc[df['Gender'] == 'M']["Sales"].iloc[0]
f_sales = df.loc[df['Gender'] == 'F']["Sales"].iloc[0]

print('The mean gap in the amount sold is:', (f_sales - m_sales) / f_sales * 100, '%')

解释如下:

  • df.loc[df['Gender']='M']
    是一个数据帧
  • 通过添加
    [“Sales”]
    选择
    “Sales”
    列,您将获得一个系列(仅包含1个元素),并且
  • 通过附加
    .iloc[0]
    可以获得该系列的第一个(=唯一的一个)元素

注:

您可以使用f-string(用于Python 3.6+)或
.format()
方法来调整输出,例如

print(f'The mean gap in the amount sold is: {(f_sales - m_sales) / f_sales * 100:.2f}%')

好的,您希望能够直接按性别对销售进行索引(使用
.loc[]
),因此我们使用
index\u col=[0]
读取您的数据帧,将索引设置为
gender
列,然后
square=True
将剩余的1列数据帧缩减为一个系列

然后我使用一个f字符串进行格式化。注意,我们可以将表达式内联到f字符串中:

import pandas as pd
from io import StringIO    

dat = """\
Gender |    Sales
___________________
M      |    25
F      |    30
"""

sl = pd.read_csv(StringIO(dat), sep='\s*\|\s*', skiprows=[1], index_col=[0],
    engine='python', squeeze=True)

#               Sales
# Gender            
# M               25
# F               30

print(f"The mean gap in the amount sold is: {100.*(1 - sl.loc['M']/sl.loc['F']):.2f}%")
# The mean gap in the amount sold is: 16.67%

# ...but f-strings even have a datatype for percent: `:.2%`, so we don't need the `100. * (...)` boilerplate.
print(f"The mean gap in the amount sold is: {(1 - sl.loc['M']/sl.loc['F']):.2%}")
The mean gap in the amount sold is: 16.67%

…如果你想更进一步,减少df->Series->dict,那么就做
sl.to_dict()
现在你可以直接引用
sl['M']/sl['F']
,就像你可能想的那样(显然我们失去了一个系列中所有丰富的方法)。

好的,你想直接按性别索引你的销售额吗(使用
.loc[]
),因此我们使用
index\u col=[0]
读取您的数据帧,将索引设置为
Gender
列,然后
squeak=True
将剩余的1列数据帧缩减为一个系列

然后我使用f字符串进行格式化。注意,我们可以将表达式内联到f字符串中:

import pandas as pd
from io import StringIO    

dat = """\
Gender |    Sales
___________________
M      |    25
F      |    30
"""

sl = pd.read_csv(StringIO(dat), sep='\s*\|\s*', skiprows=[1], index_col=[0],
    engine='python', squeeze=True)

#               Sales
# Gender            
# M               25
# F               30

print(f"The mean gap in the amount sold is: {100.*(1 - sl.loc['M']/sl.loc['F']):.2f}%")
# The mean gap in the amount sold is: 16.67%

# ...but f-strings even have a datatype for percent: `:.2%`, so we don't need the `100. * (...)` boilerplate.
print(f"The mean gap in the amount sold is: {(1 - sl.loc['M']/sl.loc['F']):.2%}")
The mean gap in the amount sold is: 16.67%

…如果你想更进一步,减少df->Series->dict,请执行
sl.to_dict()
现在你可以直接引用
sl['M']/sl['F']
,就像你可能想的那样(显然,我们失去了一系列丰富的方法。)

似乎是一种直截了当的
loc
用法。你试过了吗?这很简单。你试过什么东西了,你在哪里卡住了?你必须试一下自己的代码并发布它。这是不允许的,所以只发布一个规范并让其他人为你编写代码。这回答了你的问题吗?@smci-道歉并不是为了j保持原样,我很匆忙,忘了写我自己的代码。我已经更新了我的查询。@thatrockbottomprogrammer-否-我已经使用了
groupby()
mean()
在我之前的数据集上,为了计算平均销售额,我遇到的问题是打印出我的结果,正如我上面所解释的。这似乎是一个简单的用法。你尝试过吗?这很容易。你尝试过什么东西,你在哪里遇到的问题?你必须尝试自己的代码并发布。这是不允许的因此,只需发布一个规范并请其他人为您编写代码。这回答了您的问题吗?@smci-道歉并不是要让它保持原样,而是匆忙地忘记编写自己的代码。已更新了我的查询。@thatrockbottomprogrammer-否-我已经使用了
groupby()
mean()
在我之前计算平均销售额的数据集上,我遇到的问题是打印出我的结果,正如我上面所解释的。这很有意义-谢谢,我可以看到我缺少的内容。但是,我得到以下错误:--------------------------------------------------------------TypeError2 f_sales=df.loc[df['Gender']='f'][“sales”].iloc[0]3-->4 print中的回溯(最近一次调用上次)类型错误:不支持的操作数类型对于-:'str'和'str'我测试了它,它工作正常。您的列“Sales”中可能有字符串值(尝试
df.dtypes
查看列的数据类型)。使用
int()
float()
函数将
m\u Sales
f\u Sales
的选定值转换为数字:
m\u Sales=int(m_sales)
f_sales=int(f_sales)
打印()之前函数。是的-我想当我在沙盒中测试它时,我生成了一个新的数据框,但忘记了检查数据类型。工作起来很有魅力-特别感谢你如此简洁明了的解释-真的帮助了像我这样的初学者!非常有意义-谢谢你,我可以看到我遗漏了什么。但是,我得到了以下错误r:---------------------------------------------------------