Techno04335
Techno04335

Reputation: 1445

How to turn a Dataframe into an Array with certain specifications on Python Pandas

I have have difficult in figuring out how I can turn my specific dataframe from my excel spreadsheet df[50] into a data frame with some specifications. (I do not want the first value into the array). For example df[50] consists of:

print(df[50])

    0  50
    1  29.52
    2  29.97
    3  29.52
    4  29.97
    5  31.5
    6  33.93
    7  36.54
    8  34.02
    9  33.48
    10 32.04
    11 33.03
    12 35.01

What I would like is:

[29.52, 29.97, 29.52, 29.97, 31.5, 33.93, 36.54, 34.02, 33.48, 32.04, 33.03, 35.01]

how would i go about skipping the first value?

Thanks.

Upvotes: 0

Views: 58

Answers (2)

Uri Goren
Uri Goren

Reputation: 13700

I think this is what your looking for:

df[50].values[1:]

Upvotes: 1

jezrael
jezrael

Reputation: 863531

I use function tolist() from subset of df selected rows by position iloc[1:]:

print df[50]
#0     29.52
#1     29.97
#2     29.52
#3     29.97
#4     31.50
#5     33.93
#6     36.54
#7     34.02
#8     33.48
#9     32.04
#10    33.03
#11    35.01

List of string:

print [ '%.2f' % elem for elem in df[50].iloc[1:].tolist() ]
#['29.97', '29.52', '29.97', '31.50', '33.93', '36.54', '34.02', '33.48', '32.04', '33.03', '35.01']

List of float:
I has to use function round, because interpretation of float. More info

print [ round(elem, 2) for elem in df[50].iloc[1:].tolist() ]
#[29.97, 29.52, 29.97, 31.5, 33.93, 36.54, 34.02, 33.48, 32.04, 33.03, 35.01]

Series:

print df.iloc[1:,50]
#1     29.97
#2     29.52
#3     29.97
#4     31.50
#5     33.93
#6     36.54
#7     34.02
#8     33.48
#9     32.04
#10    33.03
#11    35.01
#Name: name, dtype: float64

Numpy array:

print np.array(df[50].iloc[1:].tolist())
#[ 29.97  29.52  29.97  31.5   33.93  36.54  34.02  33.48  32.04  33.03  35.01]

Upvotes: 1

Related Questions