- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下数据框:
Timestamp S_time1 S_time2 End_Time_1 End_time_2 Sign_1 Sign_2
0 2413044 0 0 0 0 x x
1 2422476 0 0 0 0 x x
2 2431908 0 0 0 0 x x
3 2441341 0 0 0 0 x x
4 2541232 2526631 2528631 2520631 2530631 10 80
5 2560273 2544946 2546496 2546496 2548496 40 80
6 2577224 2564010 2566010 2566010 2568010 null null
7 2592905 2580959 2582959 2582959 2584959 null null
table 就这样一直摆下去。第一列是以毫秒为单位的时间戳。 S_time1
和 End_time_1
是特定标志(数字)出现的持续时间。比如我们取第5行,S_time1
为2526631,End_time_1
为2520631,对应的sign_1
为10,表示从2526631开始到 2520631 将显示标志 10。 S_time2
和 End_time_2
也是如此。 sign_2
中的相应值将出现在从S_time2
到End_time_2
的持续时间内。
我想以 100 毫秒的 bin 时间对索引列 (Timestamp
) 重新采样,并检查符号属于哪个 bin 时间。例如,每个开始时间和结束时间之间有 2000 毫秒的差异。所以对应的标志号会在连续20个左右的bin时间重复出现,因为每个bin时间是100毫秒。所以我只需要两列:一列是垃圾时间,第二列是标志。看起来像下表:(我只是凑个bin time来举例)
Bin_time signs
...100 0
...200 0
...300 10
...400 10
...500 10
...600 10
符号10代表对应的S_time1到End_time_1的持续时间。然后下一个符号 80 在 S_time2 到 End_time_2 的持续时间内继续。我不确定这是否可以在 Pandas 中完成。但我真的需要 pandas 或其他方法的帮助。
提前感谢您的帮助和建议。
最佳答案
输入:
print df
Timestamp S_time1 S_time2 End_Time_1 End_time_2 Sign_1 Sign_2
0 2413044 0 0 0 0 x x
1 2422476 0 0 0 0 x x
2 2431908 0 0 0 0 x x
3 2441341 0 0 0 0 x x
4 2541232 2526631 2528631 2520631 2530631 10 80
5 2560273 2544946 2546496 2546496 2548496 40 80
6 2577224 2564010 2566010 2566010 2568010 null null
7 2592905 2580959 2582959 2582959 2584959 null null
2 种方法:
In [231]: %timeit s(df)
1 loops, best of 3: 2.78 s per loop
In [232]: %timeit m(df)
1 loops, best of 3: 690 ms per loop
def m(df):
#resample column Timestamp by 100ms, convert bak to integers
df['Timestamp'] = df['Timestamp'].astype('timedelta64[ms]')
df['i'] = 1
df = df.set_index('Timestamp')
df1 = df[[]].resample('100ms', how='first').reset_index()
df1['Timestamp'] = (df1['Timestamp'] / np.timedelta64(1, 'ms')).astype(int)
#felper column i for merging
df1['i'] = 1
#print df1
out = df1.merge(df,on='i', how='left')
out1 = out[['Timestamp', 'Sign_1']][(out.Timestamp >= out.S_time1) & (out.Timestamp <= out.End_Time_1)]
out2 = out[['Timestamp', 'Sign_2']][(out.Timestamp >= out.S_time2) & (out.Timestamp <= out.End_time_2)]
out1 = out1.rename(columns={'Sign_1':'Bin_time'})
out2 = out2.rename(columns={'Sign_2':'Bin_time'})
df = pd.concat([out1, out2], ignore_index=True).drop_duplicates(subset='Timestamp')
df1 = df1.set_index('Timestamp')
df = df.set_index('Timestamp')
df = df.reindex(df1.index).reset_index()
#print df.head(10)
def s(df):
#resample column Timestamp by 100ms, convert bak to integers
df['Timestamp'] = df['Timestamp'].astype('timedelta64[ms]')
df = df.set_index('Timestamp')
out = df[[]].resample('100ms', how='first')
out = out.reset_index()
out['Timestamp'] = (out['Timestamp'] / np.timedelta64(1, 'ms')).astype(int)
#print out.head(10)
#search start end
def search(x):
mask1 = (df.S_time1<=x['Timestamp']) & (df.End_Time_1>=x['Timestamp'])
#if at least one True return first value of series
if mask1.any():
return df.loc[mask1].Sign_1[0]
#check second start and end time
else:
mask2 = (df.S_time2<=x['Timestamp']) & (df.End_time_2>=x['Timestamp'])
if mask2.any():
#if at least one True return first value
return df.loc[mask2].Sign_2[0]
else:
#if all False return NaN
return np.nan
out['Bin_time'] = out.apply(search, axis=1)
#print out.head(10)
关于Python Pandas 重采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33898674/
我正在寻找一种方法来对数字进行 1:40、3812 次(长度 = 3812)的采样,并进行替换 - 但对其进行限制,使每个数字的使用次数不会超过 100 次。有没有办法在采样命令 (sample())
如果我想随机采样 pandas 数据帧,我可以使用 pandas.DataFrame.sample . 假设我随机抽取 80% 的行。如何自动获取另外 20% 未选取的行? 最佳答案 正如 Lager
我使用以下函数在每个图像中采样点。如果batch_size为None,tf.range会给出错误。如何在 tensorflow 中采样 def sampling(binary_selection,nu
我想知道是否有任何方法可以循环浏览 .wav 文件以获取 wav 文件中特定点的振幅/DB。我现在正在将它读入一个字节数组,但这对我来说没有任何帮助。 我将它与我开发的一些硬件结合使用,这些硬件将光数
我有一个日期时间的时间序列,双列存储在 mySQL 中,并且希望每分钟对时间序列进行采样(即以一分钟为间隔提取最后一个值)。在一个 select 语句中是否有一种有效的方法来做到这一点? 蛮力方式将涉
我正在为延迟渲染管道准备好我的一个小型 DirectX 11.0 项目中的一切。但是,我在从像素着色器中对深度缓冲区进行采样时遇到了很多麻烦。 首先我定义深度纹理及其着色器资源 View :
问题出现在量子值的样本上。情况是: 有一个表支付(payments): id_user[int] sum [int] date[date] 例如, sum(数量) 可以是 0 到 100,000 之间
这是一个理论问题。我目前正在研究渲染方程,我不明白在哪种情况下区域采样或半球采样更好以及为什么。 我想知道的另一件事是,如果我们采用两种方法的平均值,结果是否会更好? 最佳答案 Veach 和 Gui
我有一个 4x4 阵列,想知道是否有办法从它的任何位置随机抽取一个 2x2 正方形,允许正方形在到达边缘时环绕。 例如: >> A = np.arange(16).reshape(4,-1) >> s
我想构建 HBase 表的行键空间的随机样本。 例如,我希望 HBase 中大约 1% 的键随机分布在整个表中。执行此操作的最佳方法是什么? 我想我可以编写一个 MapReduce 作业来处理所有数据
当像这样在 GLSL 中对纹理进行采样时: vec4 color = texture(mySampler, myCoords); 如果没有纹理绑定(bind)到 mySampler,颜色似乎总是 (0
我考虑过的一些方法: 继承自Model类 Sampled softmax in tensorflow keras 继承自Layers类 How can I use TensorFlow's sampl
我有表clients,其中包含id、name、company列。 表agreements,其中包含id、client_id、number、created_at列. 一对多关系。 我的查询: SELEC
在具有许多类的分类问题中,tensorflow 文档建议使用 sampled_softmax_loss通过一个简单的 softmax减少训练时间。 根据docs和 source (第 1180 行),
首先,我想从三个数据帧(每个 150 行)中随机抽取样本并连接结果。其次,我想尽可能多地重复这个过程。 对于第 1 部分,我使用以下函数: def get_sample(n_A, n_B, n_C):
我正在尝试编写几个像素着色器以应用于类似于 Photoshop 效果的图像。比如这个效果: http://www.geeks3d.com/20110428/shader-library-swirl-p
使用 Activity Monitor/Instruments/Shark 进行采样将显示充满 Python 解释器 C 函数的堆栈跟踪。如果能看到相应的 Python 符号名称,我会很有帮助。是否有
我正在使用GAPI API来访问Google Analytics(分析),而不是直接自己做(我知道有点懒...)。我看过类文件,但看不到任何用于检查采样的内置函数。我想知道使用它的人是否找到了一种方法
我正在尝试从 Peoplesoft 数据库中随机抽取总体样本。在线搜索使我认为 select 语句的 Sample 子句可能是我们使用的一个可行选项,但是我无法理解 Sample 子句如何确定返回的样
我有一个程序,在其中我只是打印到 csv,我想要每秒正好 100 个样本点,但我不知道从哪里开始或如何做!请帮忙! from datetime import datetime import panda
我是一名优秀的程序员,十分优秀!