我无法为从一个饼图到另一个饼图的每个标签保持相同的颜色。如下图所示,Matplotlib 反转了第二个饼图中的颜色。我想为“青蛙”标签保留红色,为“猪”标签保留绿色。我也尝试添加label参数,但它只是给出了错误的计数。我还尝试反转第二张图表中的颜色,colors=colors[::-1]但它有效但不可持续,因为有时我有两个以上的标签。

这是代码:
sizes1 = ['Frogs', 'Hogs', 'Frogs', 'Frogs']
sizes2 = ['Hogs', 'Hogs', 'Hogs', 'Frogs', 'Frogs']
colors=['red', 'green']
df1 = pd.DataFrame(data=sizes1, columns=['a'])
df2 = pd.DataFrame(data=sizes2, columns=['a'])
fig, ax = plt.subplots(1, 2, figsize=(18,5))
df1['a'].value_counts().plot.pie(explode=[0,0.1],autopct='%1.1f%%',ax=ax[0],shadow=True, colors=colors)
df2['a'].value_counts().plot.pie(explode=[0,0.1],autopct='%1.1f%%',ax=ax[1],shadow=True, colors=colors)
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 csv 文件(25MB - 80000 行)导入 pandas 数据帧,但它无法正确显示。各列之间用“;”分隔 打电话时df.columns。
Originated GeoZone;Booking ...
1 PARIS;PARIS;;MARKer;EQDff;...
2 PARIS;PARIS;;MARKer;EQDff;...
3 PARIS;PARIS;;MARKer;EQDff;...
4 PARIS;PARIS;;MARKer;EQDff;...
Run Code Online (Sandbox Code Playgroud)
csv 文件在 Excel 上非常清晰。为什么熊猫没有很好地解释它。
我有两个具有相同列的数据框。只有一列具有不同的值。我想将两者串联而不重复。
df2 = pd.DataFrame({'key': ['K0', 'K1', 'K2'],'cat': ['C0', 'C1', 'C2'],'B': ['B0', 'B1', 'B2']})
df1 = pd.DataFrame({'key': ['K0', 'K1', 'K2'],'cat': ['C0', 'C1', 'C2'],'B': ['A0', 'A1', 'A2']})
df1
Out[630]:
key cat B
0 K0 C0 A0
1 K1 C1 A1
2 K2 C2 A2
df2
Out[631]:
key cat B
0 K0 C0 B0
1 K1 C1 B1
2 K2 C2 B2
Run Code Online (Sandbox Code Playgroud)
我试过了:
result = pd.concat([df1, df2], axis=1)
result
Out[633]:
key cat B key cat B
0 K0 C0 A0 K0 C0 B0 …Run Code Online (Sandbox Code Playgroud)