0

I want to load csv rows into a numpy array using pandas library. I can read the csv using pandas but havent found any function that allows reading row by row in csv file.

How do I read row by row from csv using pandas and put it into an array?

ex.

     Parch            Ticket      Fare        Cabin Embarked  
0        0         A/5 21171    7.2500          NaN        S  
1        0          PC 17599   71.2833          C85        C  

I want to create a list of lists like this:

[
[0, "A/5", 21171, 7.2500, NaN, "S"],
[0, "PC", 17599, 71.2833, "C85", "C"]
]

1 Answer 1

3

First convert to numpy array by values and then ndarray.tolist:

print (df.values)
[[0 'A/5 21171' 7.25 nan 'S']
 [0 'PC 17599' 71.2833 'C85' 'C']]

print (df.values.tolist())
[[0, 'A/5 21171', 7.25, nan, 'S'], [0, 'PC 17599', 71.2833, 'C85', 'C']]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.