希望这是一个简单的问题,但我现在无法弄明白.我想使用matplotlib来显示2个数字,然后以交互方式使用它们.我创建了数字:
import matplotlib
import pylab as pl
f1 = pl.figure()
f2 = pl.figure()
Run Code Online (Sandbox Code Playgroud)
并且可以使用类似MATLAB的pyplot接口绘制和绘制两个图形.同
current_figure = pl.gcf()
Run Code Online (Sandbox Code Playgroud)
我可以确定pyplot界面的当前活动数字,具体取决于我点击的数字.现在我想用pyplot接口绘制第一个数字,但当前数字可以是其中之一.所以有类似的东西
pl.set_current_figure(figure)
Run Code Online (Sandbox Code Playgroud)
或任何解决方法?(我知道我可以使用面向对象的界面但是对于只使用plot(x,y)等命令的交互式东西要好得多)
使用Pandas中的漂亮索引方法,我可以通过各种方式提取数据.另一方面,我仍然对如何更改现有DataFrame中的数据感到困惑.
在下面的代码中,我有两个DataFrame,我的目标是从第二个df的值更新第一个df中特定行的值.我怎样才能做到这一点?
import pandas as pd
df = pd.DataFrame({'filename' : ['test0.dat', 'test2.dat'],
'm': [12, 13], 'n' : [None, None]})
df2 = pd.DataFrame({'filename' : 'test2.dat', 'n':16}, index=[0])
# this overwrites the first row but we want to update the second
# df.update(df2)
# this does not update anything
df.loc[df.filename == 'test2.dat'].update(df2)
print(df)
Run Code Online (Sandbox Code Playgroud)
给
filename m n
0 test0.dat 12 None
1 test2.dat 13 None
[2 rows x 3 columns]
Run Code Online (Sandbox Code Playgroud)
但我怎样才能做到这一点:
filename m n
0 test0.dat 12 None
1 test2.dat 13 16 …Run Code Online (Sandbox Code Playgroud) 如果 DataFrame 中有重复值,pandas 已经提供了替换或删除重复值的功能。另一方面,在许多实验数据集中,一个人可能有“接近”的重复。
如何将这些接近重复的值替换为例如它们的平均值?
示例数据如下所示:
df = pd.DataFrame({'x': [1, 2,2.01, 3, 4,4.1,3.95, 5,],
'y': [1, 2,2.2, 3, 4.1,4.4,4.01, 5.5]})
Run Code Online (Sandbox Code Playgroud)
我试图将一些东西拼凑在一起,将接近重复的东西放在一起,但这正在使用 for 循环,似乎是对熊猫的一种黑客攻击:
def cluster_near_values(df, colname_to_cluster, bin_size=0.1):
used_x = [] # list of values already grouped
group_index = 0
for search_value in df[colname_to_cluster]:
if search_value in used_x:
# value is already in a group, skip to next
continue
g_ix = df[abs(df[colname_to_cluster]-search_value) < bin_size].index
used_x.extend(df.loc[g_ix, colname_to_cluster])
df.loc[g_ix, 'cluster_group'] = group_index
group_index += 1
return df.groupby('cluster_group').mean()
Run Code Online (Sandbox Code Playgroud)
其中分组和平均:
print(cluster_near_values(df, 'x', 0.1))
x y …Run Code Online (Sandbox Code Playgroud) 对于一个小的python脚本,我想使用临时文件和tempfile模块.不知怎的,它没有给出预期的行为,我不知道我做错了什么或者这是一个错误:
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tmp = tempfile.TemporaryFile()
>>> tmp.read()
''
>>> tmp.write('test')
>>> tmp.read()
'P\xf6D\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ [ommitted]'
Run Code Online (Sandbox Code Playgroud)
或者我只尝试了文本模式,但行为仍然很奇怪:
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tmp = tempfile.TemporaryFile('w+t')
>>> tmp.read()
''
>>> tmp.write('test')
>>> tmp.read()
'\x00\xa5\x8b\x02int or …Run Code Online (Sandbox Code Playgroud) 我想在numpy中创建一个包含数学系列值的数组,在本例中是前一个值的平方,给出一个起始值,即a_0 = 2,a_1 = 4,a_3 = 16,...
试图在numpy中使用矢量化我认为这可能有效:
import numpy as np
a = np.array([2,0,0,0,0])
a[1:] = a[0:-1]**2
Run Code Online (Sandbox Code Playgroud)
但结果是
array([2, 4, 0, 0, 0])
Run Code Online (Sandbox Code Playgroud)
我现在已经知道numpy会在内部为输出创建一个临时数组,并最终复制这个数组,这就是原始数组中零值失败的原因.有没有办法使用numpy,numexpr或其他工具来矢量化这个函数?当快速numpy函数可用而不需要for循环时,有哪些其他方法可以有效地计算一系列的值?