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

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

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

问题

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

Tag1    Tag2    Tag3    Tag4    Tag5    MobileNo
Blue    Yellow  Green   Velvet  Red     12345678
Blue    Yellow  Pink    Grey            234556778
Red     Yellow  Orange  Velvet          4456568
Red     Yellow  Grey    Blue            3454655467

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

就像这样:

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

我该如何实现这一点?

英文:

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

Tag1	Tag2	Tag3	Tag4	Tag5	MobileNo
Blue	Yellow	Green	Velvet	Red	    12345678
Blue	Yellow	Pink	Grey	        234556778
Red	    Yellow	Orange	Velvet		    4456568
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

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

How can i accomplish this?

答案1

得分: 1

使用 pandas.DataFrame.loc布尔索引

# 是否将MobileNo标记为“Red”?
m = df.filter(like="Tag").eq("Red").any(axis=1)

s = df.loc[m, "MobileNo"]

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

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

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

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

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

# is the MobileNo tagged as "Red" ?
m = df.filter(like="Tag").eq("Red").any(axis=1)

s = df.loc[m, "MobileNo"]

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

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

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

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

答案2

得分: 0

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

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

You can also use melt to flatten your tag columns:

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

答案3

得分: 0

谢谢Timeless!

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

以下是我的代码:

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

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

英文:

Thank you Timeless!

your solution worked perfectly!

Below is my code:

def readColorsDataFromClientSheet(sheetId, tag):
    ss = sheets[sheetId]
    df = ss.find('Colors').to_frame(index_col='Clients')
    tagged = df.filter(like='Tag').eq(tag).any(axis=1)
    mobile_numbers = df.loc[tagged, "MobileNo"].tolist()
    print(mobile_numbers)
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:

确定