2 回答

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊
這可能是用熊貓最簡(jiǎn)單的方法:
import pandas as pd
import datetime as dt
df = pd.read_excel('your_file_location', sheet_name='sheet_name', parse_dates=['Date'])
df = df.loc[df['Date'] == dt.date(year=2020, month=1, day=1)]
ax = df.plot.bar(df['Name'], df['Weight'])
在這里,我們首先從您的 excel 文件的特定工作表加載數(shù)據(jù)(sheet_name如果您的 excel 文件只有一個(gè)工作表,您可以省略參數(shù)),然后我們過(guò)濾數(shù)據(jù)以?xún)H顯示特定日期的記錄,然后在 x 軸上繪制名稱(chēng)和 y 軸上的權(quán)重。

TA貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('C:\\Desktop\\file.csv', index_col = 'Date',
parse_dates = True) #importing data to python and making date column as
index
df['year'] = df.index.year #extracting year from index
data_20 = df[df['year'] == 2020] # Filtering out 2020 date
ax = data_20.plot(kind='bar',x='Name',y='Weight') #Plotting by name for 2020
只為 2 人繪制:
ax = data_20[data_20['Name'] != 'John Smith']
.plot(kind='bar',x='Name',y='Weight') #Plotting by name for 2020
ax.set_ylabel('Weights in lbs') #Labeling y-axis
ax.set_xlabel('Names') #Labeling x-axis
ax.set_title('Weights for 2020') # Adding the title
為了讓它漂亮,只需添加標(biāo)簽:
ax.set_ylabel('Weights in lbs') #Labeling y-axis
ax.set_xlabel('Names') #Labeling x-axis
ax.set_title('Weights for 2020'); # Adding the title
添加回答
舉報(bào)