pandas "DataFrame"对象没有属性“地图" [英] pandas 'DataFrame' object has no attribute 'map'

查看:88
本文介绍了 pandas "DataFrame"对象没有属性“地图"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个df-df_a和df_b,

I have two df - df_a and df_b,

# df_a
number    cur    code
1000      USD    700
2000      USD    800
3000      USD    900

# df_b
number    amount    deletion code
1000      0.0       L        700
1000      10.0      X        700
1000      10.0      X        700
2000      20.0      X        800
2000      20.0      X        800
3000      0.0       L        900
3000      0.0       L        900

我想将df_adf_b合并,

df_a = df_a.merge(df_b.loc[df_b.deletion != 'L'], how='left', on=['number', 'code'])

,然后在合并结果df_a中创建一个名为deleted的标志,该标志具有三个可能的值-完全,部分和无;

and also, create a flag called deleted in the merge result df_a, that has three possible values - full, partial and none;

full-如果所有与特定number值关联的行都具有deletion = L;

full - if all rows associated with a particular number value, have deletion = L;

partial-如果某些与特定number值关联的行具有deletion = L;

partial - if some rows associated with a particular number value, have deletion = L;

none-没有与特定number值关联的行,具有deletion = L;

none - no rows associated with a particular number value, have deletion = L;

此外,在进行合并时,不应考虑来自df_bdeletion = L的行;结果看起来像是

Also when doing the merge, rows from df_b with deletion = L should not be considered; so the result looks like,

 number    amount    deletion    deleted    cur    code
 1000      10.0      X           partial    USD    700
 1000      10.0      X           partial    USD    700
 2000      20.0      X           none       USD    800
 2000      20.0      X           none       USD    800
 3000      0.0       NaN         full       USD    900

我尝试过

g = df_b['deletion'].ne('L').groupby([df_b['number'], df_b['code']])
m1 = g.any()
m2 = g.all()

d1 = dict.fromkeys(m1.index[m1 & ~m2], 'partial')
d2 = dict.fromkeys(m2.index[m2], 'full')

d = {**d1, **d2}
df_a = df_a.merge(df_b.loc[df_b.deletion != 'L'], how='left', on=['code', 'number'])

df_a['deleted'] = df_a[['number', 'code']].map(d).fillna('none')

但我遇到了错误,

AttributeError: 'DataFrame' object has no attribute 'map'

似乎df没有map功能,所以我想知道是否有其他替代方法可以实现此目的.

It seems df does not have map function, so I am wondering if there are any alternative ways to achieve this.

推荐答案

pd.DataFrame对象没有map方法.您可以改为从两列构造一个索引,然后将pd.Index.map与函数一起使用:

pd.DataFrame objects don't have a map method. You can instead construct an index from two columns and use pd.Index.map with a function:

df_a['deleted'] = df_a.set_index(['number', 'code']).index.map(d.get)
df_a['deleted'] = df_a['deleted'].fillna('none')

请注意,此处我们使用d.get而不是d.与pd.Series.map不同,pd.Index.map无法直接接受字典,但可以接受诸如dict.get的功能.

Notice we use d.get instead of d here. Unlike pd.Series.map, pd.Index.map cannot accept a dictionary directly, but it can accept a function such as dict.get.

请注意,由于pd.Index.map返回的是数组而不是序列,因此我们将fillna操作分开.

Note also we split apart the fillna operation as pd.Index.map returns an array rather than a series.

这篇关于 pandas "DataFrame"对象没有属性“地图"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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