简体   繁体   English

如何在包含多值项的 dataframe 列中获取值

[英]How to get value in dataframe columns that contains multi-value item

I have a columns like this:我有这样的专栏:

colums

how to get individual value from the column?如何从列中获取单个值?

desired output is list ex: [42008598,26472654,42054590,42774221,42444463], so it value(s) can be counted所需的 output 是列表前:[42008598,26472654,42054590,42774221,42444463],因此可以计算它的值

Let me give you an advice: when you have some example code to show us, It would be great if you paste into the code quotes like this .让我给你一个建议:当你有一些示例代码向我们展示时,如果你like this粘贴到代码引号中会很棒。 It is easiest to read.它最容易阅读。 Let's go with your question.让我们 go 与您的问题。 You can select row in a pandas dataframe like this:您可以像这样在 pandas dataframe 行中的 select 行:

import pandas as pd
print(df.iloc[i])

where i is the row number: 0, 1, 2,... and df is your dataframe.其中i是行号: 0, 1, 2,... df是您的 dataframe。 Here is the Documentation这是文档

I am also new in Stackoverflow.我也是 Stackoverflow 的新手。 I hope this could help you.我希望这可以帮助你。

What you need to convert each row in the dataframe to an array and then do the operation that you want with this array.您需要将 dataframe 中的每一行转换为数组,然后对该数组执行所需的操作。 The way you can do it with Pandas would be to declare a function that deals with each row, and them use apply to run the function each row.您可以使用 Pandas 执行此操作的方法是声明一个处理每一行的 function,然后它们使用 apply 来运行每一行的 function。

An example to count how many elements has inside each row:计算每行中有多少元素的示例:

def treat_array(row):
    row = row.replace("{", "")
    row = row.replace("}", "")
    row = row.split(",")
    return len(row)

df["Elements Count"] = df["Name of Column with the Arrays"].apply(treat_array)

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

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