Pandas DataFrame to NumPy Array: A Simple Guide
You can easily convert a Pandas DataFrame into a NumPy array using the '.values' attribute. Here's how:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6]})
array = df.values
print(array)
This code outputs:
array([[1, 4],
[2, 5],
[3, 6]], dtype=int64)
Explanation:
- Each row in the resulting array represents a sample from your DataFrame.
- Each column represents a feature.
- 'dtype=int64' indicates the data type is integer.
原文地址: https://www.cveoy.top/t/topic/ocfj 著作权归作者所有。请勿转载和采集!