基于来自其他列的段位置的Panda列中的Python Slice Ndarray

时间:2017-06-26 08:03:00

标签: python arrays pandas slice

我有一个pandas数据帧,其中一个列存储了一个2d数组:“mels” 在Dataframe的其他列中,我有列的开始和结束位置,我想从2d数组'mels'中提取。 以下是我的Dataframe的样子:

## Data Frame which has Start Location of a segment : HS_Start 
## & end location of a segment : HS_End
df_sound_loc.ix[:,-3:].head(5)[enter image description here][1]

HS_Start | HS_End | mels | --------- | ------- | ------- --- 13 | --- 25 | [[0.0752865622903,0.00439239454838,0.0182232 ... |

例如HS_Start:13且HS_End为25,那么我期待所有来自各个“mels”数组的13到25列值的行: mels [:,13:25]

依此类推所有行

# Column mels is a 2D array of 128 rows and 680 columns
df_sound_loc.ix[1,-1].shape
(128,680)

想要从mels中仅提取列:HS_Start和&之间的2d数组HS_End数字

print(df_sound_loc['mels'][:,df_sound_loc['HS_Start']:df_sound_loc['HS_End']])

出现以下错误:

如果包含密钥,则现在已经返回

ValueError: Can only tuple-index with a MultiIndex

我是Python和Dataframe操作的新手。请指教

1 个答案:

答案 0 :(得分:0)

对于按行处理,您需要apply axis=1

df1['new'] = df1.apply(lambda x: x['mels'][:, x['HS_Start']:x['HS_End']].tolist(),axis=1)