tensor in pandas in python

In pandas, you can convert a pandas Series or DataFrame object to a NumPy ndarray by using the .values attribute.

To tensor a pandas Series, you can use the following code:

main.py
import pandas as pd

# Create a pandas Series
data = pd.Series([1, 2, 3, 4, 5])

# Convert to tensor
tensor = data.values
122 chars
8 lines

To tensor a pandas DataFrame, you can use the following code:

main.py
import pandas as pd

# Create a pandas DataFrame
data = pd.DataFrame({'A': [1, 2, 3, 4, 5],
                     'B': [6, 7, 8, 9, 10]})

# Convert to tensor
tensor = data.values
179 chars
9 lines

The resulting tensor variable will be a NumPy ndarray that you can use for further analysis or processing.

related categories

gistlibby LogSnag