我被賦予了一項任務,我應該根據另一個列元素繪制一個元素。
有關更多資訊,這里是代碼:
# TODO: Plot the Male employee first name on 'Y' axis while Male salary is on 'X' axis
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_excel("C:\\users\\HP\\Documents\\Datascience task\\Employee.xlsx")
data.head(5)
輸出:
First Name Last Name Gender Age Experience (Years) Salary
0 Arnold Carter Male 21 10 8344
1 Arthur Farrell Male 20 7 6437
2 Richard Perry Male 28 3 8338
3 Ellia Thomas Female 26 4 8870
4 Jacob Kelly Male 21 4 548
假設如果我想繪制“名字”列的前 5 個,其中“性別”在 Y 軸上為男性,而“工資”列中“性別”在 X 軸上為男性。
我怎么做?
uj5u.com熱心網友回復:
首先分別生成男性行并提取名字和薪水以進行繪圖。
下面的代碼標識了前五名男性員工,并將他們的名字和薪水轉換為 x 和 y 串列。
x = list(df[df['Gender'] == "Male"][:5]['Fname'])
y = list(df[df['Gender'] == "Male"][:5]['Salary'])
print(x)
print(y)
輸出:
['Arnold', 'Arthur', 'Richard', 'Jacob']
[8344, 6437, 8338, 548]
請注意,df 中只有 4 個男性可用。
然后我們可以根據需要繪制任何圖表;
plt.bar(x, y, color = ['r', 'g', 'b', 'y']);
輸出:

uj5u.com熱心網友回復:
seaborn 也可以提供幫助
import seaborn as sns
import matplotlib.plotly as plt
sns.barplot( x=df[(df['Gender'] == "Male")]['First Name'][:5] , y = df[(df['Gender'] == "Male")]['Salary'][:5] )
plt.xlabel('First Names')
plt.ylabel('Salary')
plt.title('Barplot of Male Employees')
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/415781.html
標籤:
