简体   繁体   中英

Python Indexing with Numpy Array

I've generated my percentiles from this:

percentile = np.percentile(trace4['switchpoint'], [2.5, 97.5], axis=0)
print(percentile)

which returns

 array([[0.83300568, 0.46060436, 2.7449003 , 1.9982139 , 0.3987762 ,
    0.88365135, 0.22983616, 0.93329534, 2.72465413, 1.17089865,
    0.28492142, 0.6926268 , 0.66269826, 1.06656515, 0.46752127,
    1.76456569, 0.1313388 , 1.28500029, 1.20987646, 1.45013769,
    1.37307323, 0.32541516, 0.93211006, 0.4211398 , 0.96974685,
    1.43363548, 2.14078899, 1.51663305, 1.0689224 , 0.79560975,
    1.63287055, 2.09162316, 0.2881392 , 1.55702871, 1.48598385,
    3.55837488, 0.74913963, 1.04578597, 0.26495597, 0.51574263],
   [4.75729175, 4.1623318 , 7.14900756, 5.12070075, 6.92330205,
    7.00824703, 4.87916471, 6.8439851 , 5.05501153, 5.80100506,
    6.91067095, 5.57056179, 5.7841482 , 5.83522249, 6.90924864,
    8.13130971, 5.07952595, 7.49020049, 6.74093273, 6.95862249,
    6.88121063, 6.52230737, 6.84559638, 6.03093124, 7.11098186,
    6.11971399, 4.80683008, 5.82910868, 7.44122   , 7.82238211,
    5.64118377, 5.91967593, 7.18480546, 4.98052415, 5.04198767,
    4.22506871, 6.60798525, 6.4482244 , 5.92934079, 6.76150973]])

However, I would like to match it with my index as in "individual_id" which has the same length. I tried using

 pd.Series(percentile, index='individual_id') 

but this doesn't work and returned error message with

 "Exception: Data must be 1-dimensional". 

Can anyone help? many thanks!

percentile is a 2D array. Try:

pd.Series(percentile[0], index='individual_id')

Some extra crap since I have to write answer of greater length than is needed to answer the question asked. This site has some silly rules for helping people solve coding problems.

I agree with Ehtan's answer to solve your error.

but, by just taking the data on percentile[0], you will lose the percentile data from trace4['switchpoint'] on your second value in [2.5, 97.5].

So instead, try changing [2.5, 97.5] to a single value or just take an average. analyse why you need that 2.5 or 97.5.

so you can try something like this:

percentile = np.percentile(trace4['switchpoint'], 50, axis=0)

pd.Series(percentile, index='individual_id')

I'm not saying what you did is wrong. But it's meaningless to generate for two values and use one.

Hope it helps ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM