Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set tuple value to pandas dataframe?

Tags:

python

pandas

What i want to do should be very simple. Essentially, I have some dataframe, I need assign some tuple value to some column.

for example:

pd_tmp = pd.DataFrame(np.random.rand(3,3))
pd_tmp["new_column"] = ("a",2)

I just need a new column with tuple value, what should i do?

ValueError: Length of values does not match length of index

The previous code gets the error.

like image 314
user40780 Avatar asked Oct 28 '17 02:10

user40780


People also ask

How do you make a tuple into a DataFrame?

We need to send this list of tuples as a parameter to the pandas. DataFrame() function. The Pandas DataFrame object will store the data in a tabular format, Here the tuple element of the list object will become the row of the resultant DataFrame.

Can you store a tuple in a DataFrame?

We can create a DataFrame from a list of simple tuples, and can even choose the specific elements of the tuples we want to use.

How do you assign a value to a DataFrame column?

DataFrame - assign() function The assign() function is used to assign new columns to a DataFrame. Returns a new object with all original columns in addition to new ones. Existing columns that are re-assigned will be overwritten. The column names are keywords.

How do you set a value in a DataFrame in Python?

set_value() function put a single value at passed column and index. It takes the axis labels as input and a scalar value to be placed at the specified index in the dataframe.


2 Answers

You can wrap the tuples in a list:

import pandas as pd
pd_tmp = pd.DataFrame(np.random.rand(3,3))
pd_tmp["new_column"] = [("a",2)] * len(pd_tmp)

pd_tmp
#          0           1           2    new_column
#0  0.835350    0.338516    0.914184    (a, 2)
#1  0.007327    0.418952    0.741958    (a, 2)
#2  0.758607    0.464525    0.400847    (a, 2)
like image 145
Psidom Avatar answered Sep 19 '22 05:09

Psidom


I was looking for something similar, but in my case I wanted the tuple to be a combination of the existing columns, not just a fixed value. I found the solution below, which I share hoping it will be useful to others, like me.

In [24]: df
Out[24]:
      A     B
0     1     2
1    11    22
2   111   222
3  1111  2222

In [25]: df['D'] = df[['A','B']].apply(tuple, axis=1)

In [26]: df
Out[26]:
      A     B             D
0     1     2        (1, 2)
1    11    22      (11, 22)
2   111   222    (111, 222)
3  1111  2222  (1111, 2222)
like image 43
Stefano Paoli Avatar answered Sep 19 '22 05:09

Stefano Paoli