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.
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.
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.
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.
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.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With