filmov
tv
Convert pandas DataFrame to NumPy Array in Python (3 Examples) | Apply to_numpy() & values Attribute
Показать описание
Python code of this video:
import pandas as pd # Import pandas library in Python # Load pandas
data = pd.DataFrame({'x1':range(101, 106), # Create example DataFrame
'x2':['x', 'y', 'z', 'x', 'y'],
'x3':range(16, 11, - 1),
'x4':range(5, 10)})
print(data) # Print example DataFrame
import numpy as np # Import NumPy
print(data_array1) # Print array
# [[101 'x' 16 5]
# [102 'y' 15 6]
# [103 'z' 14 7]
# [104 'x' 13 8]
# [105 'y' 12 9]]
data_array2 = data[['x2', 'x4']].to_numpy() # Apply to_numpy to DataFrame subset
print(data_array2) # Print array
# [['x' 5]
# ['y' 6]
# ['z' 7]
# ['x' 8]
# ['y' 9]]
print(data_array3) # Print array
# [[101 'x' 16 5]
# [102 'y' 15 6]
# [103 'z' 14 7]
# [104 'x' 13 8]
# [105 'y' 12 9]]
Follow me on Social Media:
import pandas as pd # Import pandas library in Python # Load pandas
data = pd.DataFrame({'x1':range(101, 106), # Create example DataFrame
'x2':['x', 'y', 'z', 'x', 'y'],
'x3':range(16, 11, - 1),
'x4':range(5, 10)})
print(data) # Print example DataFrame
import numpy as np # Import NumPy
print(data_array1) # Print array
# [[101 'x' 16 5]
# [102 'y' 15 6]
# [103 'z' 14 7]
# [104 'x' 13 8]
# [105 'y' 12 9]]
data_array2 = data[['x2', 'x4']].to_numpy() # Apply to_numpy to DataFrame subset
print(data_array2) # Print array
# [['x' 5]
# ['y' 6]
# ['z' 7]
# ['x' 8]
# ['y' 9]]
print(data_array3) # Print array
# [[101 'x' 16 5]
# [102 'y' 15 6]
# [103 'z' 14 7]
# [104 'x' 13 8]
# [105 'y' 12 9]]
Follow me on Social Media: