第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 Python 中打印語(yǔ)句以顯示 Pandas Dataframe 上數(shù)學(xué)運(yùn)算的結(jié)果?

如何在 Python 中打印語(yǔ)句以顯示 Pandas Dataframe 上數(shù)學(xué)運(yùn)算的結(jié)果?

暮色呼如 2023-10-25 10:39:49
因此,我有一個(gè)按性別劃分的簡(jiǎn)單銷(xiāo)售額匯總數(shù)據(jù)框,內(nèi)容如下:Gender |    Sales___________________M      |    25F      |    30我現(xiàn)在想要做的就是在 Python 中返回一行內(nèi)容:銷(xiāo)售金額的平均差距為 16.67%這只是 30 - 25 除以 30,再乘以 100;我想在最后有一個(gè) % 符號(hào)。我努力了: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, '%')不幸的是這不起作用。我得到:銷(xiāo)售金額的平均差距為: 0 NaN 1 NaN 名稱:銷(xiāo)售額,dtype:對(duì)象 %請(qǐng)問(wèn)想法?我是一個(gè)非常初學(xué)者,很抱歉有這樣一個(gè)基本的查詢!
查看完整描述

2 回答

?
紅糖糍粑

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊

附加["Sales"].iloc[0]到過(guò)濾器表達(dá)式以直接獲取M和 的值F,然后將這些更改print()也投影到函數(shù)中:

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, '%')
The mean gap in the amount sold is: 16.666666666666664 %

說(shuō)明:

  • df.loc[df['Gender'] == 'M']是一個(gè)數(shù)據(jù)框;

  • "Sales"通過(guò)附加["Sales"]您獲得的系列(僅包含 1 個(gè)元素)來(lái)選擇列,并且

  • 通過(guò)附加,.iloc[0]您可以獲得該系列的第一個(gè)(=唯一一個(gè))元素。


筆記:

您可以使用 f-string (對(duì)于 Python 3.6+)或.format()調(diào)整輸出的方法,例如

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


查看完整回答
反對(duì) 回復(fù) 2023-10-25
?
斯蒂芬大帝

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

好的,您希望能夠直接按性別對(duì)您的銷(xiāo)售進(jìn)行索引(使用.loc[]),因此我們讀取您的數(shù)據(jù)幀以index_col=[0]將索引設(shè)置為Gender列,然后squeeze=True將剩余的 1 列數(shù)據(jù)幀減少為一個(gè)系列。


然后我使用 f 字符串進(jìn)行格式化。請(qǐng)注意,我們可以將表達(dá)式內(nèi)聯(lián)到 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%

...如果您想更進(jìn)一步并減少 df -> Series -> dict,請(qǐng)執(zhí)行sl.to_dict(),現(xiàn)在您sl['M']/sl['F']可以像您可能想要的那樣直接引用(顯然我們失去了 Series 的所有豐富方法。)


查看完整回答
反對(duì) 回復(fù) 2023-10-25
  • 2 回答
  • 0 關(guān)注
  • 165 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)