Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in

Posted on

Python Convert dataframe to numpy

import numpy as np
import pandas as pd

df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})

# convert to numpy array
np_array = df.values

# convert to numpy array with column names
np_array = df.values
np_array_with_col_names = np.insert(np_array, 0, df.columns, axis=0)

# convert to numpy array with row names
np_array = df.values
np_array_with_row_names = np.insert(np_array, 0, df.index, axis=1)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)