Python 将列表附加到数据帧(熊猫)
我有一个datframe,看起来像这样:Python 将列表附加到数据帧(熊猫),python,pandas,dataframe,append,Python,Pandas,Dataframe,Append,我有一个datframe,看起来像这样: id | item | amount 1 | item_a | 17 2 | item_b | 5 3 | item_c | 9 id | item | amount | price 1 | item_a | 17 | 245 2 | item_b | 5 | 189 3 | item_c | 9 | 99 我还得到了一个与
id | item | amount
1 | item_a | 17
2 | item_b | 5
3 | item_c | 9
id | item | amount | price
1 | item_a | 17 | 245
2 | item_b | 5 | 189
3 | item_c | 9 | 99
我还得到了一个与我的dataframe完全相同的入口数量的列表:
list_price = [245, 189, 99]
现在,我无法将我的列表附加到我的数据帧以创建如下内容:
id | item | amount
1 | item_a | 17
2 | item_b | 5
3 | item_c | 9
id | item | amount | price
1 | item_a | 17 | 245
2 | item_b | 5 | 189
3 | item_c | 9 | 99
我尝试过“lambda”函数,但它不起作用——或者我做错了
现在有人知道答案了吗?假设您的数据集名为df 然后,您可以使用:
list_price = [245, 189, 99]
df.insert(3,"Price",list_price)
df.head()
id | item | amount | price
1 | item_a | 17 | 245
2 | item_b | 5 | 189
3 | item_c | 9 | 99
或者,使用df[price]=list_price假设您的数据集名为df 然后,您可以使用:
list_price = [245, 189, 99]
df.insert(3,"Price",list_price)
df.head()
id | item | amount | price
1 | item_a | 17 | 245
2 | item_b | 5 | 189
3 | item_c | 9 | 99
或者,也可以使用df[price]=list_price,希望这对您有所帮助
import pandas as pd
data = {'item':['item_a', 'item_b', 'item_c'], 'amount':[17, 5, 9]}
df = pd.DataFrame(data)
list_price = [245, 189, 99]
df['price'] = list_price
print(df)
[输出]:
希望这对你有帮助
import pandas as pd
data = {'item':['item_a', 'item_b', 'item_c'], 'amount':[17, 5, 9]}
df = pd.DataFrame(data)
list_price = [245, 189, 99]
df['price'] = list_price
print(df)
[输出]:
df['price']=标价??谢谢!比我想象的简单多了!df['price']=标价??谢谢!比我想象的简单多了!