简体   繁体   English

迭代 dataframe 并为每一行分配值 - 我得到相同的值,但我想要不同的值

[英]Iterate dataframe and assign value to each row- I get the same value while I want different ones

I'm using the library isbntools to assign book titles to isbns.我正在使用图书馆 isbntools 将书名分配给 isbns。 From a dataframe that has isbns, I want to create a column named title and assign the title to the corresponding isbn.从具有 isbn 的 dataframe,我想创建一个名为 title 的列并将标题分配给相应的 isbn。 Problem is I get the same title.问题是我得到了相同的标题。

Example dataframe:示例 dataframe:

isbn

01234567

Desidred output所需 output

isbn, title 01234567, Curious George isbn, title 01234567, Curious George

Code:代码:

from isbntools.app import *

for i in range(len(df_all['isbn'])):
    for isbnz in df_all['isbn']:
        meta_dict = meta(isbnz, service='goob')
        title = meta_dict['Title']
        df_all.iloc[i, df['Title'][i]]

I tried with iloc but it seems it didn't work我试过 iloc 但它似乎没有用

Use:利用:

# isbnlib is already installed as a dependency of isbntools
import isbnlib

def get_title(isbn):
    try:
        return isbnlib.meta(isbn)['Title']
    except isbnlib.NotValidISBNError:
        return None

df['Title'] = df['isbn'].astype(str).map(get_title)
>>> df
            isbn                  Title
0       01234567                   None
1  9780007525546  The Lord Of The Rings

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为数据框中的每一行分配一个值到不同的列? - How can I assign a value to a different column for each row in a dataframe? 如何遍历 pandas dataframe 的每一行,然后有条件地在该行中设置一个新值? - How can I iterate through each row of a pandas dataframe, then conditionally set a new value in that row? 我想在 dataframe 中迭代,在另一个 dataframe 中添加值(新列) - I want to iterate in dataframe adding value( new column) in another dataframe 通过根据具有不同索引的唯一值将值从第一个数据帧更新到第二个数据帧来迭代每一行,否则追加并分配新的 ID - Iterate each row by updating values from 1st dataframe to 2nd dataframe based on unique value w/ different index, otherwise append and assign new ID 我想选择一列每一行的前 4 个单词,并根据该值使用 python 为另一个新创建的列分配一个新值 - I want to to pick the first 4 words of each row of a column and based on the value assign a new value to another newly created column using python 用另一个列的每个值迭代数据框的一行的值 - iterate a value of a row of a dataframe with each value of a column in another 如果单元格内容是一个集合并且我想查看其中是否有值,如何从熊猫数据框中有条件地获取一行? - How to conditionally get a row from a pandas dataframe, if the cell content is a set and I want to see if a value is in it? 如何为每个键分配相同的值? - How do I assign the same value for each key? Pandas Dataframe:我想将两个具有相同值的单元格合并为一个 - Pandas Dataframe: I want to merge two cells with same value into one 基于我想分离列并在不同数据框中添加值的条件 - based on a condition i want to separate the column and add the value in different dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM