I apply some functions and generate a new column values to a existing column of Pandas dataframe. However df['col1'] = new_list
does not work to assign new list to the column. Is it the wrong way and what is the accurate way to apply such operation ?
Asked
Active
Viewed 6.4k times
19

erogol
- 13,156
- 33
- 101
- 155
-
1`df` is a dictionary ? Are you receiving an error or what? Post more details please. – Nov 04 '13 at 23:08
-
2I guess `df` is a Pandas' `DataFrame`, as the title and text of the question suggests. – piokuc Nov 04 '13 at 23:21
-
2When you say it "doesn't work" could you clarify what exactly isn't working, e.g. an example and the full stacktrace. It looks like it should work. – Andy Hayden Nov 04 '13 at 23:58
-
1Yes it should've worked could you post some sample data and the errors – EdChum Nov 05 '13 at 00:00
3 Answers
19
It should work if length of the list is equal to the number of rows in the DataFrame
>>> df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6]})
>>> df['C'] = [10,20,30]
>>> df
A B C
0 1 4 10
1 2 5 20
2 3 6 30
If your list is shorter or longer than DataFrame, then you'll receive an error Length of values does not match length of index
.

Roman Pekar
- 107,110
- 28
- 195
- 197
-
2You're not re-assigning a column in your example, you're merely adding a new column to it. – Astrid Aug 31 '16 at 23:24
-
1True, yet, works also if 'C' was defined (with different values) in the first line. – gseattle Mar 30 '17 at 22:45
-
2is there a way to pad with `missing` or `nan` or `Nan` or something else? – mLstudent33 Oct 29 '20 at 03:25
2
I think this question is looking for something like this.
new_list = [1,2,3]
col1
0 [1,2,3]
1 [1,2,3]
2 [1,2,3]
And I think I found the answer here:
how to assign an entire list to each row of a pandas dataframe

codenewbie
- 128
- 6
-
You can improve your answer by adding one or more solutions from the linked page. For example: `df['col1'] = [new_list] * len(df)` – Dmitriy Work Jun 30 '22 at 09:09
0
I have prepared some more as op asked, I just didn't want to omit /cut some useful tips (still related to the adding a list as a column), hope helps someone.
import pandas as pd
from tabulate import tabulate
# Module tabulate is optional just for pretty printing of the Data Frame / need install if not installed i.e: pip install tabulate
# Table1 (in terminal screen): Add a list during creation of the Data Frame #
list1 = ['apple', 'banana', 'orange', 'avocado', 'melon', 'papaya']
df1 = pd.DataFrame(list1,columns = ['Fruits'])
print(tabulate(df1,headers='keys',tablefmt='pretty'),'\n') # ==> Or just print(df1) little messy
# Table2 : Add a list as a new column to an existing Data Frame #
list2 = ['3$','4$','2.5$','10$','5$','12$']
df1['Prices'] = list2
print(tabulate(df1,headers='keys',tablefmt='pretty'),'\n')
# Table3 : Small Trick for part4 : Add only the column name with some dummy data #
df1['Amount(Kg)'] = '0'
print(tabulate(df1,headers='keys',tablefmt='pretty'),'\n')
# Table4 : Now you can assign your existing list to an existing column #
list3 = [100,90,80,70,60,50]
df1['Amount(Kg)'] = list3
print(tabulate(df1,headers='keys',tablefmt='pretty'),'\n')
Will generate the following output :
+---+---------+
| | Fruits |
+---+---------+
| 0 | apple |
| 1 | banana |
| 2 | orange |
| 3 | avocado |
| 4 | melon |
| 5 | papaya |
+---+---------+
+---+---------+--------+
| | Fruits | Prices |
+---+---------+--------+
| 0 | apple | 3$ |
| 1 | banana | 4$ |
| 2 | orange | 2.5$ |
| 3 | avocado | 10$ |
| 4 | melon | 5$ |
| 5 | papaya | 12$ |
+---+---------+--------+
+---+---------+--------+------------+
| | Fruits | Prices | Amount(Kg) |
+---+---------+--------+------------+
| 0 | apple | 3$ | 0 |
| 1 | banana | 4$ | 0 |
| 2 | orange | 2.5$ | 0 |
| 3 | avocado | 10$ | 0 |
| 4 | melon | 5$ | 0 |
| 5 | papaya | 12$ | 0 |
+---+---------+--------+------------+
+---+---------+--------+------------+
| | Fruits | Prices | Amount(Kg) |
+---+---------+--------+------------+
| 0 | apple | 3$ | 100 |
| 1 | banana | 4$ | 90 |
| 2 | orange | 2.5$ | 80 |
| 3 | avocado | 10$ | 70 |
| 4 | melon | 5$ | 60 |
| 5 | papaya | 12$ | 50 |
+---+---------+--------+------------+
If the op had added a line like in Section 3 above, his code would also work. But As I said I wanted you to see and visualize other steps too.
df['col1'] = '0'
df['col1'] = new_list

everyt4u
- 314
- 3
- 5