0

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.

Joe the Second
  • 339
  • 1
  • 11
  • Does this answer your question? [Python pandas insert list into a cell](https://stackoverflow.com/questions/26483254/python-pandas-insert-list-into-a-cell) – Mykola Zotko Dec 25 '20 at 22:30

2 Answers2

2

You can pass list constructor as an aggregating function, along axis 1 (index)

data['New_column'] = data.agg(list, axis=1)

Outputs:

     C1    C2    C3    C4               New_column
0  0.98  1.25  1.30  1.00   [0.98, 1.25, 1.3, 1.0]
1  1.10  0.99  1.41  0.99  [1.1, 0.99, 1.41, 0.99]
Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34
0

You can use the insert function for inserting a new column. This should solve your issue.

def append_new_column():
data = {"C1": [0.98,  1.10], "C2": [1.25,  0.99],
    "C3": [1.3,   1.41], "C4": [1.00,  .99] }
data = pd.DataFrame(data)
new_column = []
for i in range(len(data)):
    new_column.append(data.iloc[i,0:4].values)
data.insert(len(data.columns), "New Column", new_column, True)
return data
Altan
  • 1
  • 1