如何获取由pandas.DataFrame.plot创建的绘图的内部创建的colorbar实例?
以下是生成彩色散点图的示例:
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] )
plt.show()
Run Code Online (Sandbox Code Playgroud)
如何获取colorbar实例以便操作它,例如更改标签或设置刻度?
Is there a nice way to prevent the conversion of entities to python objects while loading a YAML string the yaml package? Particularily, I do not want the conversion of timestamp-strings to datetime objects.
Here is an example:
import yaml
yaml.load("""d: 2018-06-17\nn: 42""")
Run Code Online (Sandbox Code Playgroud)
which gives
{'d': datetime.date(2018, 6, 17), 'n': 42}
Run Code Online (Sandbox Code Playgroud)
but I would like to have
{'d': '2018-06-17', n: 42}
Run Code Online (Sandbox Code Playgroud)
其中日期字符串保留为字符串和其他类型的转换。我不想更改输入字符串,例如,通过指定特定数据类型。也许有一个替代的 YAML 加载器/解析器包。我正在使用 python3.6 和 PyYAML==3.12。
是否可以以某种方式将pandas.drop_duplicates与比较运算符一起使用,该比较运算符比较特定列中的两个对象以识别重复项?如果不是,还有什么替代方案?
这是一个可以使用它的示例:
我有一个 pandas DataFrame,其中列出了特定列中的值,我希望根据列删除重复项A
import pandas as pd
df = pd.DataFrame( {'A': [[1,2],[2,3],[1,2]]} )
print df
Run Code Online (Sandbox Code Playgroud)
给我
A
0 [1, 2]
1 [2, 3]
2 [1, 2]
Run Code Online (Sandbox Code Playgroud)
df.drop_duplicates( 'A' )
Run Code Online (Sandbox Code Playgroud)
给我一个TypeError
[...]
TypeError: type object argument after * must be a sequence, not itertools.imap
Run Code Online (Sandbox Code Playgroud)
然而我想要的结果是
A
0 [1, 2]
1 [2, 3]
Run Code Online (Sandbox Code Playgroud)
我的比较函数在这里:
def cmp(x,y):
return x==y
Run Code Online (Sandbox Code Playgroud)
但原则上它可能是别的东西,例如,
def cmp(x,y):
return x==y and len(x)>1
Run Code Online (Sandbox Code Playgroud)
如何根据比较函数有效地删除重复项?
更重要的是,如果我有更多列要分别使用不同的比较函数进行比较,我该怎么办?