我的数据框看起来像这样,我现在正在寻找合适的分析技术,我希望这里的一些人可以给我一些关于从这个数据框创建图表或提取“最大”和“最小”的建议。
网站 | A | B | C | D |
---|---|---|---|---|
蓝色 | 4 | 13 | 9 | 11 |
绿色 | 1 | 12 | 30 | 20 |
黄色 | 12 | 2 | 3 | 3 |
红色 | 20 | 14 | 4 | 0 |
我首先想为每个站点制作饼图来描述每个站点的比率或条形图, 然后编写代码,允许输出具有最大和最小“蓝色”、“绿色”等频率的站点
我尝试过: p = df[["C"]].value_counts(sort="False").plot(kind='bar') 没有效果,并且已经尝试过这个,但我不确定如何引用“1”所在的颜色值。
x_axis = df.iloc[1].values
y_axis = df.iloc[1].values
plt.bar(x_axis,y_axis,color = 'b')
plt.xlabel('Site')
plt.ylabel('Colour')
plt.show()
您可以一次性完成所有这些绘图:
import pandas as pd
import matplotlib.pyplot as plt
data = {
'Site': ['Blue', 'Green', 'Yellow', 'Red'],
'A': [4, 1, 12, 20],
'B': [13, 12, 2, 14],
'C': [9, 30, 3, 4],
'D': [11, 20, 3, 0]
}
df = pd.DataFrame(data)
df.set_index('Site', inplace=True)
def create_plots_grid(df):
sites = df.index
fig, axes = plt.subplots(nrows=len(sites), ncols=2, figsize=(12, 12))
for i, site in enumerate(sites):
data = df.loc[site]
data.plot.pie(autopct='%1.1f%%', startangle=90, ax=axes[i, 0])
axes[i, 0].set_ylabel('')
axes[i, 0].set_title(f'Pie Chart for {site} Site')
data.plot.bar(ax=axes[i, 1])
axes[i, 1].set_title(f'Bar Chart for {site} Site')
axes[i, 1].set_xlabel('Category')
axes[i, 1].set_ylabel('Value')
plt.tight_layout()
plt.show()
create_plots_grid(df)
max_sites = df.idxmax()
min_sites = df.idxmin()
print("Site with maximum values for each column:")
print(max_sites)
print("\nSite with minimum values for each column:")
print(min_sites)
这给出了
和
Site with maximum values for each column:
A Red
B Red
C Green
D Green
dtype: object
Site with minimum values for each column:
A Green
B Yellow
C Yellow
D Red
dtype: object