1import pandas as pd
2
3#initialize a dataframe
4df = pd.DataFrame(
5 [[21, 72, 67],
6 [23, 78, 69],
7 [32, 74, 56],
8 [52, 54, 76]],
9 columns=['a', 'b', 'c'])
10
11#convert dataframe to numpy array
12arr = df.to_numpy()
13
14print('\nNumpy Array\n----------\n', arr)
1>>> pd.DataFrame({"A": [1, 2], "B": [3, 4]}).to_numpy()
2array([[1, 3],
3 [2, 4]])
4
1import numpy as np
2import pandas as pd
3
4
5arr = np.random.rand(4, 3)
6print("Numpy array:")
7print(arr)
8
9# convert numpy array to dataframe
10df = pd.DataFrame(arr, columns =['A', 'B', 'C'])
11print("\nPandas DataFrame: ")
12df
13