我有一个pandas DataFrame,类似于:
col1 col2 col3 col5
NaN 1 2 8
2 NaN 4 8
4 NaN 4 8
Run Code Online (Sandbox Code Playgroud)
我想做两件事:
1)合并第1列和第2列:
newcol1 col3 col5
1 2 8
2 4 8
4 4 8
Run Code Online (Sandbox Code Playgroud)
我尝试过使用.concat,但这只是连接行.似乎我不能使用+具有NaN值的标准运算符.
2)从新的第1列和第3列中减去第5列,所以我最终得到:
newcol1 col3
-7 -6
-6 -4
-4 -4
Run Code Online (Sandbox Code Playgroud)
试过这样做:
dataframe[['newcol1', 'col2']] - dataframe['col5']
Run Code Online (Sandbox Code Playgroud)
和
dataframe[['newcol1', 'col2']].subtract(dataframe['col5'])
Run Code Online (Sandbox Code Playgroud)
但都不起作用.
我有一个数据框,其中包含一个组ID,两个距离度量(经度/纬度类型度量)和一个值.对于给定的一组距离,我想找到附近其他组的数量,以及附近其他组的平均值.
我编写了以下代码,但效率很低,以至于它无法在合理的时间内完成非常大的数据集.附近零售商的计算很快.但附近零售商平均价值的计算极其缓慢.有没有更好的方法来提高效率?
distances = [1,2]
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)),
columns=['Group','Dist1','Dist2','Value'])
# get one row per group, with the two distances for each row
df_groups = df.groupby('Group')[['Dist1','Dist2']].mean()
# create KDTree for quick searching
tree = cKDTree(df_groups[['Dist1','Dist2']])
# find points within a given radius
for i in distances:
closeby = tree.query_ball_tree(tree, r=i)
# put into density column
df_groups['groups_within_' + str(i) + 'miles'] = [len(x) for x in closeby]
# get average values of nearby groups
for idx, val in enumerate(df_groups.index): …Run Code Online (Sandbox Code Playgroud) 我有一个1,000,000x 50Pandas DataFrame,我目前正在使用以下方法写入SQL表:
df.to_sql('my_table', con, index=False)
这需要非常长的时间.我已经看到了关于如何在线加速这个过程的各种解释,但它们似乎都不适用于MSSQL.
如果我尝试以下方法:
使用SQLAlchemy批量插入Pandas DataFrame
然后我收到一个no attribute copy_from错误.
如果我尝试多线程方法:
http://techyoubaji.blogspot.com/2015/10/speed-up-pandas-tosql-with.html
然后我收到一个QueuePool limit of size 5 overflow 10 reach, connection timed out错误.
有没有简单的方法来加速to_sql()到MSSQL表?要么是通过BULK COPY还是其他一些方法,而是完全来自Python代码?
我有一个主要的 python 脚本,它启动一个在后台运行的线程。
poll = threading.Thread(target=poll_files, args=myargs)
我希望我的主脚本等到我的poll线程中发生特定的事情。我想使用一个 Event 对象。所以在我的主脚本中,我这样做:
trigger = threading.Event()
当我想等待时:
trigger.wait()
我的问题是,在我的poll线程中,如何将事件设置为 True?我知道我这样做:
trigger.set()
但我还需要trigger = threading.Event()在我的poll线程里面吗?
我有一个矩阵a.shape: (80000, 38, 38).我要检查,看看是否有任何重复或类似(38,38)沿着第一维度的矩阵(在这种情况下,有这些矩阵80000).
我可以通过两个for循环:
for i in range(a.shape[0]):
for g in range(a.shape[0]):
if a[i,:,:] - a[g,:,:] < tolerance:
# save the index here
Run Code Online (Sandbox Code Playgroud)
但这看起来非常低效.我知道有numpy.unique,但是当我有一组二维矩阵时,我不确定我是否理解它是如何工作的.
建议有效地做到这一点?有没有办法让广播找到所有矩阵中所有元素的差异?
我有一个字符串,表示全年,然后是一年中的ISO周(因此某些年份有53周,因为周计数从一年的第一个完整周开始)。我想使用将其转换为datetime对象pandas.to_datetime()。所以我做:
pandas.to_datetime('201145', format='%Y%W')
Run Code Online (Sandbox Code Playgroud)
它返回:
Timestamp('2011-01-01 00:00:00')
Run Code Online (Sandbox Code Playgroud)
这是不对的。或者,如果我尝试:
pandas.to_datetime('201145', format='%Y%V')
Run Code Online (Sandbox Code Playgroud)
它告诉我这%V是一个错误的指令。
我究竟做错了什么?
cross_val_score我正在通过向其传递一个对象来运行嵌套交叉验证GridSearchCV。然后我跟进cross_val_predict以获得绘图的模型预测。像这样:
gs = GridSearchCV(mymodel, myparams)
score = cross_val_score(gridsearch, X_train, y_train)
prediction = cross_val_predict(gs, X_train, y_train)
Run Code Online (Sandbox Code Playgroud)
这在计算上似乎是多余的;有没有一种方法可以从中获取交叉验证的预测cross_val_score,或者我是否需要手动迭代 CV 对象的折叠才能一步完成此操作?
我有一个ipython noteboook,我正在运行一个需要很长时间的过程.我%R在大部分时间里使用ipython 魔术,所以我不能轻易地将笔记本转换为python脚本.
有没有办法可以打开我的笔记本,运行全部,然后关闭我的浏览器并断开与终端的连接,仍然让笔记本在后台运行,我可以在以后连接?
我在Stack Exchange上看到有关保持内核活动的信息,但我对如何与笔记本中运行的实际代码进行交互感到困惑.
我有一个数据框df:
df = pd.DataFrame({'id1':[1,1,1,1,1,4,4,4,6,6],
'id2':[45,45,33,33,33,1,1,1,34,34],
'vals':[0.1,0.2,0.6,0.1,0.15,0.34,0.12,0.5,0.4,0.45],
'date':pd.to_datetime(['2017-01-01','2017-01-02','2017-01-01',
'2017-04-01','2017-04-02','2017-01-01',
'2017-01-02','2017-01-03','2017-01-04',
'2017-01-05'])})
Run Code Online (Sandbox Code Playgroud)
我想根据时间来产生延迟方面各组的id1和id2。例如,t_1将是前一天的值。t_2是两天前的值。如果前两天没有任何价值,我希望是nan。这将是上述数据帧的输出:
date id1 id2 vals t_1 t_2
0 2017-01-01 1 33 0.60 NaN NaN
1 2017-04-01 1 33 0.10 NaN NaN
2 2017-04-02 1 33 0.15 0.10 NaN
0 2017-01-01 1 45 0.10 NaN NaN
1 2017-01-02 1 45 0.20 0.10 NaN
0 2017-01-01 4 1 0.34 NaN NaN
1 2017-01-02 4 1 0.12 0.34 NaN
2 …Run Code Online (Sandbox Code Playgroud) 我正在寻找找到两个不同大小的矩阵的交集的最有效方法。每个矩阵都有三个变量(列)和不同数量的观察值(行)。例如,矩阵A:
a = np.matrix('1 5 1003; 2 4 1002; 4 3 1008; 8 1 2005')
b = np.matrix('7 9 1006; 4 4 1007; 7 7 1050; 8 2 2003'; 9 9 3000; 7 7 1000')
Run Code Online (Sandbox Code Playgroud)
如果我将每列的公差设置为col1 = 1,,col2 = 2和col3 = 10,则需要一个函数,使其输出in a和in b分别在各自公差之内,例如:
[x1, x2] = func(a, b, col1, col2, col3)
print x1
>> [2 3]
print x2
>> [1 3]
Run Code Online (Sandbox Code Playgroud)
您可以通过索引看到的元素2 a在的元素1的公差内b。
我想我可以遍历矩阵的每个元素a,检查它是否在的每个元素的公差范围内b,然后这样做。但是,对于非常大的数据集而言,效率似乎很低。 …
python ×10
pandas ×5
numpy ×4
performance ×4
dataframe ×2
matrix ×2
background ×1
date ×1
datetime ×1
duplicates ×1
events ×1
import ×1
intersection ×1
ipython ×1
jupyter ×1
linux ×1
scikit-learn ×1
search ×1
sql ×1
string ×1