私信  •  关注

CT Zhu

CT Zhu 最近创建的主题
CT Zhu 最近回复了
9 年前
回复了 CT Zhu 创建的主题 » 如何在Python中更改Colorbar标签?

pandas 不返回颜色条的轴,因此我们必须找到它:

首先,让我们获得 figure 实例:即使用 plt.gcf()

In [61]:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import itertools as it

# [ (0,0), (0,1), ..., (9,9) ]
xy_positions = list( it.product( range(10), range(10) ) )

df = pd.DataFrame( xy_positions, columns=['x','y'] )

# draw 100 floats
df['score'] = np.random.random( 100 )

ax = df.plot( kind='scatter',
              x='x',
              y='y',
              c='score',
              s=500)
ax.set_xlim( [-0.5,9.5] )
ax.set_ylim( [-0.5,9.5] )

f = plt.gcf()

2.这个数字有多少轴?

In [62]:

f.get_axes()
Out[62]:
[<matplotlib.axes._subplots.AxesSubplot at 0x120a4d450>,
 <matplotlib.axes._subplots.AxesSubplot at 0x120ad0050>]

3.第一个轴(即创建的第一个轴)包含绘图

In [63]:

ax
Out[63]:
<matplotlib.axes._subplots.AxesSubplot at 0x120a4d450>

因此,第二个轴是色条轴

In [64]:

cax = f.get_axes()[1]
#and we can modify it, i.e.:
cax.set_ylabel('test')
10 年前
回复了 CT Zhu 创建的主题 » 如何在python pandas中的列中删除重复的名称[重复]

实际上,删除行0和1只需要(保留包含匹配a和c的任何观察结果):

In [335]:

df['AC']=df.A+df.C
In [336]:

print df.drop_duplicates('C', take_last=True) #this dataset is a special case, in general, one may need to first drop_duplicates by 'c' and then by 'a'.
     A  B  C    AC
2  foo  1  B  fooB
3  bar  1  A  barA

[2 rows x 4 columns]

但我怀疑你真正想要的是这个(保留一个包含匹配a和c的观察结果):

In [337]:

print df.drop_duplicates('AC')
     A  B  C    AC
0  foo  0  A  fooA
2  foo  1  B  fooB
3  bar  1  A  barA

[3 rows x 4 columns]

编辑:

现在情况更清楚了,因此:

In [352]:
DG=df.groupby(['A', 'C'])   
print pd.concat([DG.get_group(item) for item, value in DG.groups.items() if len(value)==1])
     A  B  C
2  foo  1  B
3  bar  1  A

[2 rows x 3 columns]