Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Machine Learning by (4k points)
edited by
Could someone tell me how do I convert a pandas dataframe to a numpy array using python?

1 Answer

0 votes
by (9.6k points)
edited by

There are two methods of going ahead with this, one is by hardcoding the data frame, which might get a little tedious when it comes to large datasets. The other way around is to use the functions provided by pandas. 

Here is an explanation for both the methods: 

You can try hardcoding it like this 

array([[nan, 0.7, nan],
       [nan, 0.65, 0.5],
       [nan, 0.2, 0.5],
       [0.1, 0.2, nan],
       [0.1, 0.2, 0.9],
       [0.6, nan, 0.5],
       [0.1, nan, nan]])

The other way is to use a pandas function for it. These functions work on converting numPy arrays from al sorts of panda objects. This came out with pandas v0.24.0

  1. to_numpy(): Works for index, series and data frame
  2. array: works with index and series only 
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94.6k users

Browse Categories