我有一个熊猫数据帧如下。你知道吗
title details
0 cake recipe cake recipe comes here
1 bread recipe bread recipe comes here
2 chocolate recipe chocolate recipe comes here
3 biscuit recipe biscuit recipe comes here
4 beans recipe beans recipe comes here
5 waffle recipe waffle recipe comes here
6 pudding recipe pudding recipe comes here
我还有一个清单如下。你知道吗
mylist = [5, 2, 0]
我想得到title
和details
中的mylist
项。所以,我的输出如下所示。你知道吗
mydetails = ['waffle recipe. waffle recipe comes here', 'chocolate recipe chocolate recipe comes here', 'cake recipe cake recipe comes here']
我现在的代码如下。你知道吗
mydetails = []
v = df.loc[df.index.isin(mylist)]
item_details= v.title + '. ' + v.details
for val in item_details:
mydetails.append(val)
然而,我得到的v
是错误的。请告诉我怎么修?你知道吗
您可以首先按^{} 选择行,然后将连接的列转换为
list
:在代码中使用^{} ,因此默认值的顺序是:
要获得正确的输出,请删除
isin
:可以使用
list comprehension
创建所需的列表:输出:
df.values
是所有行作为列表的列表。"".join
用于连接每行的两个列值。你知道吗要在两个条目之间添加句点和空格,可以使用
". ".join()
或者,可以根据需要添加逗号或分号。你知道吗
相关问题 更多 >
编程相关推荐