【问题标题】:Python Slice Ndarray with in Panda column based on segment location from Other columnsPython Slice Ndarray 在 Panda 列中基于来自其他列的段位置
【发布时间】:2017-11-29 00:48:24
【问题描述】:

我有一个带有 2d 数组的 pandas 数据框存储在其中一列中:“mels” 在数据框的其他列中,我想从二维数组“mels”中提取列的开始和结束位置。 这是我的数据框的样子:

## 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_开始 | HS_结束 |梅尔斯 | ---------| --------|------- ---13 | ---25 | [[0.0752865622903, 0.00439239454838, 0.0182232... |

例如 HS_Start: 13 和 HS_End 是 25,那么我期望来自各个“mels”数组的所有行具有 13 到 25 列值: 梅尔斯[​​:,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 和 HS_End 数字之间的二维数组

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

出现以下错误:

如果包含 key,现在应该已经返回了

ValueError: Can only tuple-index with a MultiIndex

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

【问题讨论】:

  • 欢迎来到 StackOverflow。请花时间阅读how to provide a great pandas example 上的这篇文章以及如何提供minimal, complete, and verifiable example 并相应地修改您的问题。 how to ask a good question 上的这些提示也可能有用。
  • HS_START HS_END MELS 0 13 25 [0.018223254838,0.018223945838,0.0183923945838,0.018223254838,0.018223254838,0.01822399838,0.0182232,0.0182232,0.0182232,0.0182232,0.0182232,0.0182232,0.0182232,0.0182232。 3 86 98 [[0.0752865622903, 0.00439239454838, 0.0182232... 4 117 129 [[0.0752865622903, 0.00439239454838, 0.0182232...
  • 请用数据编辑问题。
  • 最好的方法是创建所需的输出。
  • @jezrael 添加了数据示例和期望的结果。

标签: python arrays pandas slice


【解决方案1】:

您需要applyaxis=1 才能按行处理:

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

【讨论】:

    猜你喜欢
    • 2015-03-30
    • 1970-01-01
    • 2016-03-05
    • 2016-12-27
    • 1970-01-01
    • 2018-06-10
    • 2020-11-01
    • 2011-10-13
    • 2018-09-26
    相关资源
    最近更新 更多