我应用一些函数来生成Pandas数据帧(dataframe)中现有列的新列值。但是,使用df['col1'] = new_list
将新列表分配给该列不起作用。这是错误的方法吗?如果不是,有什么准确的方法可以执行此操作?
如果列表的长度等于数据框中的行数,则应该可以工作
>>> 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
如果您的列表长度与DataFrame长度不同,那么您将收到一个错误消息Length of values does not match length of index
。我认为这个问题想要的是这样的内容。
new_list = [1,2,3]
col1
0 [1,2,3]
1 [1,2,3]
2 [1,2,3]
我认为我在这里找到了答案:
df['col1'] = [new_list] * len(df)
。 - Dmitriy Work我已经按照op的要求准备了更多内容,只是不想省略/删减一些有用的提示(仍然与将列表作为列添加相关),希望能帮助到某些人。
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')
+---+---------+
| | 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 |
+---+---------+--------+------------+
如果 OP 在上面的第 3 部分添加了一行代码,他的代码也会起作用。 但正如我所说,我希望您也能看到和想象其他步骤。
df['col1'] = '0'
df['col1'] = new_list
df
是一个字典吗?你是否收到了错误信息?请发布更多细节。 - user1301404df
是Pandas的DataFrame
,正如问题的标题和文本所示。 - piokuc