获取基于另一列的列值,其中包含 Pandas 数据框中的字符串列表 [英] get column value based on another column with list of strings in pandas dataframe

查看:67
本文介绍了获取基于另一列的列值,其中包含 Pandas 数据框中的字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了链接.但它不适用于我下面给出的示例.我尝试使用 loc[0] 作为输出.我试过.item().但这些都没有帮助我.

<预><代码>>>>df2 = pd.DataFrame({ 'Item':['[Phone]', '[Watch]', '[Pen]', '[Pencil]', '[Knife]'], 'RelatedItem': ['[手机盖]', '[表带]', '[笔帽]', '[铅笔芯]', '[叉子]'], 'CountinInventory':['20','50','40','80','90']})>>>df2项目相关项目盘点库存0 [手机] [手机壳] 201 [手表] [表带] 502 [笔] [笔帽] 403【铅笔】【铅笔芯】80支4【刀】【叉】90>>>df2.loc[df2['Item'] == 'Phone', 'RelatedItem']系列([],名称:相关项目,数据类型:对象)>>>df2.loc[df2['Item'] == 'Phone', 'RelatedItem', 'CountinInventory']pandas.core.indexing.IndexingError:索引器太多

我有这个数据,当我输入 Phone 时,我需要获取 Phone coverCountinInventory 值作为我的答案.请建议我在这里做错了什么.

解决方案

我相信你需要 str 来删除第一个和最后一个 [] 或使用 str.strip:

mask = df2['Item'].str[1:-1] == 'Phone'#替代方案#mask = df2['Item'].str.strip('[]') == '电话'打印(面具)0 真1 错误2 错误3 错误4 错误名称:项目,数据类型:bool

如果没有缺失值是可能的,使用list comprehension,如果大数据更快:

mask = [x[1:-1] == 'Phone'for x in df2['Item']]mask = [x.strip('[]') =='Phone'for x in df2['Item']]打印(面具)[对,错,错,错,错]

最后选择多列使用list:

df3 = df2.loc[mask, ['RelatedItem', 'CountinInventory']]打印 (df3)相关物品盘点库存0 【手机壳】20

I tried the link. But it doesnt work for my example given below. I tried the loc[0] for the output. I tried .item(). But none of these help me.

>>> df2 = pd.DataFrame({ 'Item':['[Phone]', '[Watch]', '[Pen]', '[Pencil]', '[Knife]'], 'RelatedItem': ['[Phone cover]', '[Watch strap]', '[Pen cap]', '[Pencil lead]', '[fork]'], 'CountinInventory':['20','50','40','80','90']})
>>> df2
    Item     RelatedItem   CountinInventory
0   [Phone]  [Phone cover]               20
1   [Watch]  [Watch strap]               50
2     [Pen]      [Pen cap]               40
3  [Pencil]  [Pencil lead]               80
4   [Knife]         [fork]               90
>>> df2.loc[df2['Item'] == 'Phone', 'RelatedItem']
Series([], Name: RelatedItem, dtype: object)
>>> df2.loc[df2['Item'] == 'Phone', 'RelatedItem', 'CountinInventory']
pandas.core.indexing.IndexingError: Too many indexers

I have this data where when I feed Phone, I need to get Phone cover along with the CountinInventory value as my answer. Please advice what mistake am I doing here.

解决方案

I believe you need str for remove first and last [] or use str.strip:

mask = df2['Item'].str[1:-1] == 'Phone'
#alternative solution
#mask = df2['Item'].str.strip('[]') == 'Phone'

print (mask)
0     True
1    False
2    False
3    False
4    False
Name: Item, dtype: bool

If no missing values is possible use list comprehension, what is faster if large data:

mask = [x[1:-1] == 'Phone'for x in df2['Item']]

mask = [x.strip('[]') == 'Phone'for x in df2['Item']]
print (mask)

[True, False, False, False, False]

Last for select multiple columns use list:

df3 = df2.loc[mask, ['RelatedItem', 'CountinInventory']]
print (df3)
     RelatedItem CountinInventory
0  [Phone cover]               20

这篇关于获取基于另一列的列值,其中包含 Pandas 数据框中的字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆