How do I convert a Pandas dataframe to a PyTorch tensor?
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?
import pandas as pd
import torch
import random
# creating dummy targets (float values)
targets_data = [random.random() for i in range(10)]
# creating DataFrame from targets_data
targets_df = pd.DataFrame(data=targets_data)
targets_df.columns = ['targets']
# creating tensor from targets_df
torch_tensor = torch.tensor(targets_df['targets'].values)
# printing out result
print(torch_tensor)
Output:
tensor([ 0.5827, 0.5881, 0.1543, 0.6815, 0.9400, 0.8683, 0.4289,
0.5940, 0.6438, 0.7514], dtype=torch.float64)
Tested with Pytorch 0.4.0.
I hope this helps, if you have any further questions - just ask. :)
pytorch tensor from pandas columns of vectors
If you have this dataframe which each column is a vector of 2 numbers:
import torch
import pandas as pd
df = pd.DataFrame({'a': [[ 3, 29],[ 3, 29]],
'b': [[94, 170],[ 3, 29]],
'c': [[31, 115],[ 3, 29]]})
To convert this dataframe to a pytorch tensor, you only need to convert the values of dataframe to list and then a tensor:
t = torch.Tensor(list(df.values))
#output
tensor([[[ 3., 29.],
[ 94., 170.],
[ 31., 115.]],
[[ 3., 29.],
[ 3., 29.],
[ 3., 29.]]])
The shape of t
is [2,3,2] is 2 rows, 3 columns, 2 elements inside each list.
Convert list of two dimensional DataFrame to Torch Tensor
I don't think you can convert the list of dataframes in a single command, but you can convert the list of dataframes into a list of tensors and then concatenate the list.
E.g.
import pandas as pd
import numpy as np
import torch
data = [pd.DataFrame(np.zeros((5,50))) for x in range(100)]
list_of_arrays = [np.array(df) for df in data]
torch.tensor(np.stack(list_of_arrays))
#or
list_of_tensors = [torch.tensor(np.array(df)) for df in data]
torch.stack(list_of_tensors)
Related Topics
Converting Json into Newline Delimited Json in Python
Python3: Remove a Substring Between Two Delimiting Char
Replace Empty Strings With None/Null Values in Dataframe
Python - How to Sort a List of Alpha and Numeric Values
Json.Loads() Decodes Only With Raw String Literal
Reading Contents of a Gzip File from a Aws S3 in Python
How to Click on an Element from the Dropdown Menu Through Python and Selenium
Regex That Matches a Number With Commas for Every Three Digits
Splitting Dictionary Items into Smaller Dictionaries Based on Condition
How to Track the Number of Times a Function Is Called
Robot Framework Using Python, Key Press Without Selecting Any Button or Element in the Page
Replacing Pandas or Numpy Nan With a None to Use With Mysqldb
How to Write a Lambda Function That Is Conditional on Two Variables (Columns) in Python
How to Install Tesseract for Python on Anaconda
How to Split a CSV File Row to Columns in Python
How to Skip Blank Line While Reading CSV File Using Python