1

I have a pandas dataframe with an 2d array stored in one of the columns: "mels" In other columns of the Dataframe I have start and end location of columns I will like to extract from 2d array 'mels'. Here is how my Dataframe looks like:

## 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... |

For Example HS_Start: 13 and HS_End is 25, then I am expecting all rows with 13 to 25 column values from respective "mels" array: mels[:,13:25]

so on and so forth for all rows

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

Want to extract only the Columns from mels: 2d array between HS_Start & HS_End numbers

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

Got following error:

If key is contained, would have returned by now

ValueError: Can only tuple-index with a MultiIndex

I am new to Python and Dataframe operations. please advise

Spandan
  • 27
  • 1
  • 8
  • 1
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Jun 26 '17 at 08:04
  • HS_Start HS_End mels 0 13 25 [[0.0752865622903, 0.00439239454838, 0.0182232... 1 34 46 [[0.0752865622903, 0.00439239454838, 0.0182232... 2 62 74 [[0.0752865622903, 0.00439239454838, 0.0182232... 3 86 98 [[0.0752865622903, 0.00439239454838, 0.0182232... 4 117 129 [[0.0752865622903, 0.00439239454838, 0.0182232... – Spandan Jun 26 '17 at 08:04
  • Please edit question with data. – jezrael Jun 26 '17 at 08:05
  • Also the best is create desired output. – jezrael Jun 26 '17 at 08:06
  • @jezrael added data example and desired outcome. – Spandan Jun 26 '17 at 08:20
  • Is possible use `df1['new'] = df1.apply(lambda x: x['mels'][:, x['HS_Start']:x['HS_End']].tolist(),axis=1)` ? – jezrael Jun 26 '17 at 09:14
  • @jezrael Thanks for the input, works like a charm – Spandan Jun 27 '17 at 06:56

1 Answers1

0

You need apply with axis=1 for process by rows:

df1['new'] = df1.apply(lambda x: x['mels'][:, x['HS_Start']:x['HS_End']].tolist(),axis=1)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252