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

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

Create numpy array from panda daataframe inside a For loop

问题

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

  1. 让我们假设我有以下的数据框
  2. data = {"Names": ["Ray", "John", "Mole", "Smith", "Jay", "Marc", "Tom", "Rick"],
  3. "Sports": ["Soccer", "Judo", "Tenis", "Judo", "Tenis","Soccer","Judo","Tenis"]}
  4. 我想要使用一个循环对于每个独特的运动项目我可以获取一个包含参与该运动项目的人的numpy数组在伪代码中可以解释为
  5. for unique sport in sports:
  6. nArray = 包含练习该运动的人的numpy数组
  7. ---------
  8. nArray执行某些操作
  9. -------

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

英文:

Lets say that i have the following dataframe:

  1. data = {"Names": ["Ray", "John", "Mole", "Smith", "Jay", "Marc", "Tom", "Rick"],
  2. "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

  1. for unique sport in sports:
  2. nArray= numpy array of names of people practicing sport
  3. ---------
  4. Do something with nArray
  5. -------

答案1

得分: 0

使用 GroupBy.applynp.array

  1. df = pd.DataFrame(data)
  2. s = df.groupby('Sports')['Names'].apply(np.array)
  3. print (s)
  4. Sports
  5. Judo [John, Smith, Tom]
  6. Soccer [Ray, Marc]
  7. Tenis [Mole, Jay, Rick]
  8. Name: Names, dtype: object
  9. for sport, name in s.items():
  10. print (name)
  11. ['John' 'Smith' 'Tom']
  12. ['Ray' 'Marc']
  13. ['Mole' 'Jay' 'Rick']
英文:

Use GroupBy.apply with np.array:

  1. df = pd.DataFrame(data)
  2. s = df.groupby('Sports')['Names'].apply(np.array)
  3. print (s)
  4. Sports
  5. Judo [John, Smith, Tom]
  6. Soccer [Ray, Marc]
  7. Tenis [Mole, Jay, Rick]
  8. Name: Names, dtype: object
  9. for sport, name in s.items():
  10. print (name)
  11. ['John' 'Smith' 'Tom']
  12. ['Ray' 'Marc']
  13. ['Mole' 'Jay' 'Rick']

答案2

得分: 0

一种方法是

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

one way to go

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

答案3

得分: 0

  1. import numpy as np
  2. import pandas as pd
  3. data = {"Names": ["Ray", "John", "Mole", "Smith", "Jay", "Marc", "Tom", "Rick"],
  4. "Sports": ["Soccer", "Judo", "Tenis", "Judo", "Tenis", "Soccer", "Judo", "Tenis"]}
  5. df = pd.DataFrame(data)
  6. unique_sports = df['Sports'].unique()
  7. for sport in unique_sports:
  8. uniqueNames = np.array(df[df['Sports'] == sport]['Names'])
  9. print(uniqueNames)

Result:

['Mole' 'Jay' 'Rick']

英文:

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

  1. import numpy as np
  2. import pandas as pd
  3. data = {"Names": ["Ray", "John", "Mole", "Smith", "Jay", "Marc", "Tom", "Rick"],
  4. "Sports": ["Soccer", "Judo", "Tenis", "Judo", "Tenis","Soccer","Judo","Tenis"]}
  5. df = pd.DataFrame(data)
  6. unique_sports = df['Sports'].unique()
  7. for sport in unique_sports:
  8. uniqueNames = np.array(df[df['Sports'] == sport]['Names'])
  9. print(uniqueNames)

Result :

  1. ['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:

确定