Home > OS >  Slicing arrays inside pd dataframe cells
Slicing arrays inside pd dataframe cells

Time:12-18

I have a dataframe looking like this:

                                            spectrum  concentration
0  [-0.7966700525900023, 1.051812899165725, -3.22...          97.21
1  [4.2516158928053756, 4.311297642065483, 0.5998...           9.16
2  [2.6277027502790133, 7.421702513385412, -7.280...         184.42
3  [-9.030692948962951, -11.021414125284082, -3.8...          77.61
4  [-7.057630112337506, -4.877704649740394, -10.8...         109.02

and need to select the range [70:920] of each of the arrays in the spectrum collumn. I tried the following:

df.spectrum.apply(lambda x: x[70:920])

but it didn't work. Any idea how to do it?

CodePudding user response:

I would normally use .map() for this, e.g.,

df.spectrum.map(lambda x: x[70:920])

CodePudding user response:

You can extract your range in a vectorized way:

df['spectrum2'] = list(np.array(df['spectrum'].to_numpy().tolist())[:, 70:920])
  • Related