Skip to content
Advertisement

How do I convert a Pandas dataframe to a PyTorch tensor?

How do I train a simple neural network with PyTorch on a pandas dataframe df?

The column df["Target"] is the target (e.g. labels) of the network. This doesn’t work:

JavaScript

Advertisement

Answer

I’m referring to the question in the title as you haven’t really specified anything else in the text, so just converting the DataFrame into a PyTorch tensor.

Without information about your data, I’m just taking float values as example targets here.

Convert Pandas dataframe to PyTorch tensor?

JavaScript

Output:

JavaScript

Tested with Pytorch 0.4.0.

I hope this helps, if you have any further questions – just ask. :)

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement