Shameendra
Shameendra

Reputation: 182

Store the value from pandas dataframe without index or header

I am trying to get the values from a CSV file using python and pandas. To avoid index and headers i am using .values with iloc but as an output my value is stored in [] brackets. I dont want them i just need the value. I dont want to print it but want to use it for other operations.

My code is :

import pandas as pd
ctr_x = []
ctr_y = []
tl_list = []
br_list = []
object_list = []
img = None

obj = 'red_hat'
df = pd.read_csv('ring_1_05_sam.csv')
ctr_x = df.iloc[10:12, 0:1].values #to avoid headers and index
ctr_y = df.iloc[10:12, 1:2].values #to avoid headers and index

ctr_x =[]
ctr_y =[]

If i print the result of ctr_x and ctr_y to check if correct values are recorded The output i get is :

[[1536.25]
[1536.5 ]]
[[895.25]
[896.  ]]

So i short i am getting the correct values but i don't want the brackets. Can anyone please suggest any other alternatives to my method. Note : I dont want to print the values but store it(without index and headers) for further operations

Upvotes: 0

Views: 11051

Answers (1)

Vaishali
Vaishali

Reputation: 38415

When you use column slice, pandas returns a Dataframe. Try

type(df.iloc[10:12, 0:1])
pandas.core.frame.DataFrame

This in turn will return a 2-D array when you use

df.iloc[10:12, 0:1].values

If you want a 1 dimensional array, you can use integer indexing which will return a Series,

type(df.iloc[10:12, 0])
pandas.core.series.Series

And a one dimensional array,

df.iloc[10:12, 0].values

So use

ctr_x = df.iloc[10:12, 0].values
ctr_y = df.iloc[10:12, 1].values

Upvotes: 2

Related Questions