pandas 'DataFrame' 对象没有属性 'map'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51744786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:54:18  来源:igfitidea点击:

pandas 'DataFrame' object has no attribute 'map'

pythonpython-3.xpandasdictionarydataframe

提问by daiyue

I have two df - df_a and df_b,

我有两个 df - df_a 和 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

I want to left merge df_awith df_b,

我想左合并df_adf_b

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

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

并且,创建一个deleted在合并结果中调用的标志,该标志df_a具有三个可能的值 - full、partial 和 none;

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

full- 如果所有行都与特定number值相关联,则有deletion= L;

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

partial- 如果某些行与特定number值相关联,则有deletion= L;

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

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

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

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

 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

I tried,

我试过,

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')

but I got an error,

但我有一个错误,

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

It seems dfdoes not have mapfunction, so I am wondering if there are any alternative ways to achieve this.

它似乎df没有map功能,所以我想知道是否有其他方法可以实现这一点。

回答by jpp

pd.DataFrameobjects don't have a mapmethod. You can instead construct an index from two columns and use pd.Index.mapwith a function:

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

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

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

注意我们d.getd这里使用了代替。与 不同pd.Series.mappd.Index.map不能直接接受字典,但可以接受诸如dict.get.

Note also we split apart the fillnaoperation as pd.Index.mapreturns an array rather than a series.

另请注意,我们将fillna操作拆分为pd.Index.map返回一个数组而不是一个系列。