使用Pandas Dataframe的.loc函数找到的数值写入一个数组中。

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

Write Values found by Pandas Dataframe's .loc function into an array

问题

我有一个Google电子表格,成功加载到了一个Pandas数据框中:

  1. Tag1 Tag2 Tag3 Tag4 Tag5 MobileNo
  2. Blue Yellow Green Velvet Red 12345678
  3. Blue Yellow Pink Grey 234556778
  4. Red Yellow Orange Velvet 4456568
  5. Red Yellow Grey Blue 3454655467

现在我不太熟悉Pandas。
我需要将所有在它们的行中具有一个标签的MobileNo写入一个数组中。

就像这样:

  1. tag_red_results = ['12345678', '4456568', '3454655467']

我该如何实现这一点?

英文:

I have a google spreadsheet which i managed to load into a pandas dataframe:

  1. Tag1 Tag2 Tag3 Tag4 Tag5 MobileNo
  2. Blue Yellow Green Velvet Red 12345678
  3. Blue Yellow Pink Grey 234556778
  4. Red Yellow Orange Velvet 4456568
  5. Red Yellow Grey Blue 3454655467

Now i am not really familiar with pandas.
I would need all MobileNo which have a tag in one of the 5 tag columns within their rows to be written into an array.

Like

  1. tag_red_results = ['12345678', '4456568', '3454655467']

How can i accomplish this?

答案1

得分: 1

使用 pandas.DataFrame.loc布尔索引

  1. # 是否将MobileNo标记为“Red”?
  2. m = df.filter(like="Tag").eq("Red").any(axis=1)
  3. s = df.loc[m, "MobileNo"]

如果需要一个列表,可以使用 pandas.Series.to_list

  1. tag_red_results = s.to_list()
  2. #[12345678, 4456568, 3454655467]

或者,如果你需要一个NumPy数组,可以使用 pandas.Series.to_numpy

  1. tag_red_results = s.to_numpy()
  2. #array([ 12345678, 4456568, 3454655467], dtype=int64)
英文:

IIUC, use pandas.DataFrame.loc with boolean indexing :

  1. # is the MobileNo tagged as "Red" ?
  2. m = df.filter(like="Tag").eq("Red").any(axis=1)
  3. s = df.loc[m, "MobileNo"]

If a list is needed, then use pandas.Series.to_list :

  1. tag_red_results = s.to_list()
  2. #[12345678, 4456568, 3454655467]

Or, if you need a numpy array, use pandas.Series.to_numpy :

  1. tag_red_results = s.to_numpy()
  2. #array([ 12345678, 4456568, 3454655467], dtype=int64)

答案2

得分: 0

你还可以使用 melt 来展开你的标签列:

  1. >>> df.melt('MobileNo').loc[lambda x: x['value'] == 'Red', 'MobileNo'].tolist()
  2. [4456568, 3454655467, 12345678]
英文:

You can also use melt to flatten your tag columns:

  1. >>> df.melt('MobileNo').loc[lambda x: x['value'] == 'Red', 'MobileNo'].tolist()
  2. [4456568, 3454655467, 12345678]

答案3

得分: 0

谢谢Timeless!

你的解决方案完美地运行了!

以下是我的代码:

  1. def readColorsDataFromClientSheet(sheetId, tag):
  2. ss = sheets[sheetId]
  3. df = ss.find('Colors').to_frame(index_col='Clients')
  4. tagged = df.filter(like='Tag').eq(tag).any(axis=1)
  5. mobile_numbers = df.loc[tagged, "MobileNo"].tolist()
  6. print(mobile_numbers)
  7. return mobile_numbers

请注意,这里的代码部分没有进行翻译。

英文:

Thank you Timeless!

your solution worked perfectly!

Below is my code:

  1. def readColorsDataFromClientSheet(sheetId, tag):
  2. ss = sheets[sheetId]
  3. df = ss.find('Colors').to_frame(index_col='Clients')
  4. tagged = df.filter(like='Tag').eq(tag).any(axis=1)
  5. mobile_numbers = df.loc[tagged, "MobileNo"].tolist()
  6. print(mobile_numbers)
  7. return mobile_numbers

huangapple
  • 本文由 发表于 2023年1月9日 01:33:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049963.html
匿名

发表评论

匿名网友

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

确定