I have a dataset as following:
data = {"C1": [0.98, 1.10], "C2": [1.25, 0.99],
"C3": [1.3, 1.41], "C4": [1.00, .99] }
data = pd.DataFrame(data)
C1 C2 C3 C4
0 0.98 1.25 1.30 1.00
1 1.10 0.99 1.41 0.99
I want to have a new column which is a list
of all numbers in the same row. So my desired output would be as following:
C1 C2 C3 C4 New_Column
0 0.98 1.25 1.30 1.00 [0.98, 1.25, 1.30, 1.00]
1 1.10 0.99 1.41 0.99 [1.10, 0.99, 1.41, 0.99]
I already developed the following code:
data["New_Column"] = np.nan
def create_list_of_numbers(data):
for i in range(len(data)-1):
print(data.iloc[i,:4].values) # Gives the list of values of first four columns
data.iloc[i,dt.columns.get_loc("New_Column")] = data.iloc[i,:4].values
return data
I am getting the following error: "ValueError: setting an array element with a sequence."
Even if this functions works I think there should be a way using apply
function so I can get rid of this loop.