简体   繁体   English

基于一列列表访问数据框的行。 如果列表中存在唯一字符串,则将查看该行

[英]Access rows of dataframe based on a column of lists. If a unique string is inside the list, the row will be be viewed

This is difficult to explain, so I haven't been able to google my problem.这很难解释,所以我无法用谷歌搜索我的问题。

I have a dataframe.我有一个数据框。 A column of that dataframe contains lists.该数据框的一列包含列表。 Each row has a list of strings.每行都有一个字符串列表。 The lists are of various size.列表大小不一。 Some rows don't have a list, but a NaN value.有些行没有列表,但有 NaN 值。

I want to be able to view rows of the dataframe that contain an arbitrary string in their list.我希望能够查看在其列表中包含任意字符串的数据框行。 So if I want to find all rows that have a list that contains "arbitrary_string" as an element of the list, those rows will be selected.因此,如果我想查找包含“arbitrary_string”作为列表元素的列表的所有行,将选择这些行。

Here is an image indicating an example dataframe.这是指示示例数据帧的图像。

I want to use the term "corndog" to return a view of row 1 and 2. The location in the list of the string does not matter.我想使用术语“corndog”来返回第 1 行和第 2 行的视图。字符串列表中的位置无关紧要。 My associates suggested I try to use lambdas and apply and a special function together.我的同事建议我尝试使用 lambdas 和 apply 以及一个特殊的函数。 Their examples haven't worked for me.他们的例子对我不起作用。

They propose:他们提议:

def find_id(inpList:list,inpstr):  
    print(inpList)
    for x in inpList:
        if inpstr in x:
            return(1)
    return(0)

Df[list_of_strings].apply(lambda x: find_id(x, cust string))

I'm not really sure what I'm doing.我不确定我在做什么。 I don't understand how these things could be pieced together.我不明白这些东西怎么能拼凑起来。

IIUC, I think you can use this: IIUC,我想你可以用这个:

Original df:原始df:

+----+-----------+--------------+----------------------------+
|    | some_int  | some_string  |      List_of_strings       |
+----+-----------+--------------+----------------------------+
| 0  |       84  | something    | [‘cat’,’dog’,’corndog’]    |
| 1  |       74  | etc          | [‘qwetry’,’celphone’]      |
| 2  |       64  | etc          | [‘dog’,corndog’]           |
| 3  |       89  | etc          | [‘etc’,’catfish’,’purple’] |
+----+-----------+--------------+----------------------------+

df[df['List_of_strings'].str.contains('corndog')]

Output:输出:


    some_int    some_string  List_of_strings
0   84          something   [‘cat’,’dog’,’corndog’]
2   64              etc     [‘dog’,corndog’]

EDIT considering column value are of list type and not string you can use following:编辑考虑到列值是列表类型而不是字符串,您可以使用以下内容:

df[df['List_of_strings'].apply(lambda x: 'corndog' in x)]

暂无
暂无

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

相关问题 Pandas - Dataframe 具有带列表的列。 如何对列表中的元素进行分组? - Pandas - Dataframe has column with lists. How can I groupby the elements within the list? 将列表中的元素与包含列表的列相匹配。 如果找到单个元素,则返回整行 - Matching an element from a list to a column that holds lists. If single element found, return entire row 将带有字符串和列表的元组列表转换为列表列表。 [('str', [0, 0])...] 到 [['str', 0, 0]...] - Convert list of tuple with string and list to the list of lists. [('str', [0, 0])...] to [['str', 0, 0]...] 我有一个带有特定列的列表的所有行。 从目标列表中选择不包含至少一个元素的行 - I have a all the rows with a particular column with lists. Select rows that does not contain atleast one element from the target list 基于现有列的唯一值(列表)的 Pandas DataFrame 中的新列 - new column in pandas DataFrame based on unique values (lists) of an existing column 使用基于(非唯一)列值的其他行中的值替换 DataFrame 行中的 NaN 值 - Replacing NaN values in a DataFrame row with values from other rows based on a (non-unique) column value 从多个列表和列表列表中获取唯一对象。 然后使用所有列表中的唯一对象创建一个新列表 - Get unique objects from multiple lists and lists of lists. Then create a new list with unique objects from all the lists 熊猫-根据列表列中的唯一值合并行 - Pandas - consolidating rows based on unique values in column of lists 数据框根据行类型将行的平衡分配应用于列表 - Dataframe apply balanced allocation of rows to lists based on row type 如何使用数据框列中的唯一值创建列表列表? - How to create a list of lists using unique values in a dataframe column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM