Reputation: 13614
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 ?
Upvotes: 21
Views: 74894
Reputation: 352
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
Upvotes: 0
Reputation: 138
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
Upvotes: 3
Reputation: 117345
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
.
Upvotes: 21