简体   繁体   English

如何使用 Pandas 访问列中的特定行

[英]How to access a specific row in a column using Pandas

folks!伙计们!

I'm stuck while developing a dashboboard using Pandas.我在使用 Pandas 开发仪表板时卡住了。 This is the scenario:这是场景:

I'm importing and transforming a CSV file in order to get some insights about a team I am working with.我正在导入和转换 CSV 文件,以便了解我正在与之合作的团队。

|ID      |Area Path                             | 
|--------|--------------------------------------|
| 544    | [Level 1, Level 2, Level 3]          |
| 545    | [Level 1, Level 2]                   |
| 546    | [Level 1]                            |
| 547    | [Level 1, Level 2, Level 3, Level 4] | 

As you can see, the column Area Path does not have a pattern.如您所见, Area Path列没有模式。 Sometimes I'll find a list with 1 or 2 or 3 or 4 items on it.有时我会找到一个包含 1 或 2 或 3 或 4 个项目的列表。

I'm facing a problem in order to access each line in this collumn to get the information I need.为了访问此列中的每一行以获取我需要的信息,我遇到了一个问题。 If the list has only one item, I must use the [0] position, if the list has 2 or more items, I must use the [1] position.如果列表只有一项,我必须使用[0] position,如果列表有2个或更多项目,我必须使用[1] position。

I've tried to do different things and this one below is my last attempt:我尝试做不同的事情,下面这个是我最后一次尝试:

def Extract(lst):
    if dados['Area Path'].str.len() == 1:
      return [item[0] for item in dados['Area Path']]
    elif dados['Area Path'].str.len() == 2:
      return [item[-1] for item in dados['Area Path']]
    elif dados['Area Path'].str.len() == 3:
      return [item[1] for item in dados['Area Path']]
    elif dados['Area Path'].str.len() == 4:
      return [item[1] for item in dados['Area Path']]

lst = [dados['Area Path']]
indice_novo = Extract(lst)
dados['Team'] = indice_novo

The problem is that I'm not able to iterate over the list that is the column.问题是我无法遍历作为列的列表。 The output provided by .str.len() is great, but it does not help me completely. .str.len()提供的 output 很棒,但它并不能完全帮助我。

Can you help me to solve this problem?你能帮我解决这个问题吗?

Thanks, Marcelo谢谢,马塞洛

Here is a solution using map()这是使用map()的解决方案

df['Area Path'].map(lambda x: x[0] if len(x) == 1 else x[1])

Output: Output:

0    Level 2
1    Level 2
2    Level 1
3    Level 2
Name: Area Path, dtype: object

Based on your comment, the Area Path column contains lists.根据您的评论, Area Path列包含列表。 If so, you are accessing the columns incorrectly.如果是这样,您访问的列不正确。 The correct way to access the lists in the columns would be:访问列中列表的正确方法是:

lst = dados['Area Path'].tolist()

This will populate the lst variable with a list of lists, which looks something like:这将使用列表列表填充lst变量,如下所示:

[['Level 1', 'Level 2', 'Level 3'], ['Level 1', 'Level 2'], ['Level 1'], ...]

Then, in your Extract() function, you can perform your filtering based on the required logic:然后,在您的 Extract() function 中,您可以根据所需的逻辑执行过滤:

def Extract(list_of_lists):
    new_list = []
    for lst in list_of_lists:
        # Will fail if 'Area Path' contains None, NaN values
        if len(lst) == 1:
            new_list.append(lst[0])
        else:
            new_list.append(lst[1])
    return new_list

indice_novo = Extract(lst)
dados['Team'] = indice_novo

This answer is based on your code and may not be the most optimized way to do this.此答案基于您的代码,可能不是最优化的方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM