从Pandas数据框内部的for循环中创建NumPy数组。

huangapple go评论70阅读模式
英文:

Create numpy array from panda daataframe inside a For loop

问题

以下是翻译好的代码部分:

让我们假设我有以下的数据框

data = {"Names": ["Ray", "John", "Mole", "Smith", "Jay", "Marc", "Tom", "Rick"],
        "Sports": ["Soccer", "Judo", "Tenis", "Judo", "Tenis","Soccer","Judo","Tenis"]}

我想要使用一个循环对于每个独特的运动项目我可以获取一个包含参与该运动项目的人的numpy数组在伪代码中可以解释为

for unique sport in sports:
    nArray = 包含练习该运动的人的numpy数组
    ---------
    对nArray执行某些操作
    -------

请注意,代码中的伪代码部分没有被翻译,只有注释和字符串被翻译。

英文:

Lets say that i have the following dataframe:

data = {"Names": ["Ray", "John", "Mole", "Smith", "Jay", "Marc", "Tom", "Rick"],
        "Sports": ["Soccer", "Judo", "Tenis", "Judo", "Tenis","Soccer","Judo","Tenis"]}

I want to have a for loop like that for each unique Sport i am able to retrieve a numpy array containing the Names of people playing that sport. In pseudo code that can be explainded as

for unique sport in sports:
    nArray= numpy array of names of people practicing sport
    ---------
    Do something with nArray
    -------

答案1

得分: 0

使用 GroupBy.applynp.array

df = pd.DataFrame(data)
s = df.groupby('Sports')['Names'].apply(np.array)
print (s)
Sports
Judo      [John, Smith, Tom]
Soccer           [Ray, Marc]
Tenis      [Mole, Jay, Rick]
Name: Names, dtype: object

for sport, name in s.items():
    print (name)

    ['John' 'Smith' 'Tom']
    ['Ray' 'Marc']
    ['Mole' 'Jay' 'Rick']
英文:

Use GroupBy.apply with np.array:

df = pd.DataFrame(data)
s = df.groupby('Sports')['Names'].apply(np.array)
print (s)
Sports
Judo      [John, Smith, Tom]
Soccer           [Ray, Marc]
Tenis      [Mole, Jay, Rick]
Name: Names, dtype: object

for sport, name in s.items():
    print (name)

    ['John' 'Smith' 'Tom']
    ['Ray' 'Marc']
    ['Mole' 'Jay' 'Rick']

答案2

得分: 0

一种方法是

df = pd.DataFrame(data)
for sport in df.Sports.unique():
    list_of_names = list(df[df.Sports == sport].Names)
    data = np.array(list_of_names)
英文:

one way to go

df = pd.DataFrame(data)
for sport in df.Sports.unique():
    list_of_names = list(df[df.Sports == sport].Names)
    data = np.array(list_of_names)

答案3

得分: 0

import numpy as np
import pandas as pd

data = {"Names": ["Ray", "John", "Mole", "Smith", "Jay", "Marc", "Tom", "Rick"],
        "Sports": ["Soccer", "Judo", "Tenis", "Judo", "Tenis", "Soccer", "Judo", "Tenis"]}

df = pd.DataFrame(data)

unique_sports = df['Sports'].unique()

for sport in unique_sports:
    uniqueNames = np.array(df[df['Sports'] == sport]['Names'])

print(uniqueNames)

Result:

['Mole' 'Jay' 'Rick']

英文:

You can do by pandas library for get list array of sport persons name.

import numpy as np
import pandas as pd

data = {"Names": ["Ray", "John", "Mole", "Smith", "Jay", "Marc", "Tom", "Rick"],
        "Sports": ["Soccer", "Judo", "Tenis", "Judo", "Tenis","Soccer","Judo","Tenis"]}

df = pd.DataFrame(data)

unique_sports = df['Sports'].unique()

for sport in unique_sports:
    uniqueNames = np.array(df[df['Sports'] == sport]['Names'])

print(uniqueNames)

Result :

['Mole' 'Jay' 'Rick']

huangapple
  • 本文由 发表于 2023年2月6日 19:14:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定