Code_Art
Code_Art

Reputation: 121

Python: How to get values and not object type while iterating through pandas Data Frame?

I am trying to fetch values of each row of pandas dataframe using below code.

points = [ makePoint(row) for row in df.iterrows() ]

here df has 4 column. each contains integer data.

while i try to print row, it returns follow,

1    3
2    3
3    4
4    5
Name: 0, dtype: int64

I just need

[3,3,4,5] 

and don't want index, name and dtype.

Upvotes: 0

Views: 410

Answers (1)

jezrael
jezrael

Reputation: 863501

Use tolist() if need convert Series to list and iterrows return tuples - index with Series, so add i for unpacking it:

points = [ makePoint(row).tolist() for i, row in df.iterrows() ]

Or list:

points = [ list(makePoint(row)) for i, row in df.iterrows() ]

Another solution:

points = df.apply(makePoint, axis=1).values.tolist()

Upvotes: 0

Related Questions