Add tensor to a dataframe

Viewed 591

I have a tensor with the following characteristics:

<class 'torch.Tensor'>

torch.Size([600, 1])

I would like it to be a column in a data frame.

When I add the tensor to a df as is, I get the following for one of the rows.

tensor([-0.1186])

How do I get rid of the tensor([])? So that each row is just a number. For this row, I would just want -0.1186.

1 Answers

If you squeeze your tensor from size (600,1) to size (600) you can directly add its values as a new column to a DataFrame:

df['newcol'] = my_tensor.squeeze()
Related