0

I have a data frame in hand, now i want to add one more column in that data frame. that column will be a set of lists, ie, if the length of the data frame is 10, the column will be set of 10 lists. Each row associate with one list. How can i make this? And initially those lists will be blank list so that i can append objects further.

  • Does this answer your question? [Adding new column to existing DataFrame in Python pandas](https://stackoverflow.com/questions/12555323/adding-new-column-to-existing-dataframe-in-python-pandas) – Roshin Raphel Jul 19 '20 at 08:33

3 Answers3

0

I can answer your question with this code(length of df is 3 and not 10)

    import pandas as pd

    df = pd.DataFrame([{'Name': 'Chris', 'Item Purchased': 'Sponge', 'Cost': 22.50},
               {'Name': 'ABC', 'Item Purchased': 'Chocolate', 'Cost': 2.50},
               {'Name': 'Filip', 'Item Purchased': 'Spoon', 'Cost': 5.00}],
              index=['Store 1', 'Store 1', 'Store 2'])
    df
    df['Date'] = [['December 1','December 10'], ['January 1','February 5'], ['mid- 
    May','mid-September']]
    df

The image shows the output output

0

A new column in a pandas Dataframe can be added as simple as :

df['new_column_name'] = your_list

If your_list is nested, you can append new values into its elements, by the usual append function (for example, into your_list[0] by df['new_column_name'][0].append(value))

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
0

I can give the answer of my question i think. Suppose i have a dataset called XYZ, It has @ column A,B. Now i want to add one more column C which is a set of lists ie, there are same number of lists as number of rows. XYZ = pd.read_csv('untitled.csv', usecols = ['A', 'B']) XYZ['C'] = pd.Series([]*len(XYZ), dtype = object) for i in range(len(XYZ)): XYZ['C'][i] = []