Python 使用字典映射数据帧索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43356704/
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
Map dataframe index using dictionary
提问by ChuHo
Why doesn't df.index.map(dict)work like df['column_name'].map(dict)?
为什么不df.index.map(dict)工作df['column_name'].map(dict)?
Here's a little example of trying to use index.map:
下面是一个尝试使用 index.map 的小例子:
import pandas as pd
df = pd.DataFrame({'one': {'A': 10, 'B': 20, 'C': 30, 'D': 40, 'E': 50}})
map_dict = {'A': 'every', 'B': 'good', 'C': 'boy', 'D': 'does', 'E': 'fine'}
df
'''
one
A 10
B 20
C 30
D 40
E 50
'''
df['two'] = df.index.map(mapper=map_dict)
This raises TypeError: 'dict' object is not callable
这引发 TypeError: 'dict' object is not callable
Feeding it a lambda works:
喂它一个 lambda 的工作原理:
df['two'] = df.index.map(mapper=(lambda x: map_dict[x])); df
'''
one two
A 10 every
B 20 good
C 30 boy
D 40 does
E 50 fine
'''
However, resetting the index and mapping on a column works as expected without complaint:
但是,重置列上的索引和映射按预期工作而不会抱怨:
df.reset_index(inplace=True)
df.rename(columns={'index': 'old_ndx'}, inplace=True) #so there's no index name confusion
df['two'] = df.old_ndx.map(map_dict); df
'''
old_ndx one two
0 A 10 every
1 B 20 good
2 C 30 boy
3 D 40 does
4 E 50 fine
'''
回答by piRSquared
I'm not answering your question... Just giving you a better work around.
Use to_series()them map
我不是在回答你的问题......只是给你一个更好的解决方法。
使用to_series()它们map
df = pd.DataFrame({'one': {'A': 10, 'B': 20, 'C': 30, 'D': 40, 'E': 50}})
map_dict = {'A': 'every', 'B': 'good', 'C': 'boy', 'D': 'does', 'E': 'fine'}
df['two'] = df.index.to_series().map(map_dict)
df
one two
A 10 every
B 20 good
C 30 boy
D 40 does
E 50 fine
回答by YOBEN_S
Adding getat the end
get在最后添加
df['Two']=df.index.map(map_dict.get)
df
Out[155]:
one Two
A 10 every
B 20 good
C 30 boy
D 40 does
E 50 fine
回答by T. Ray
An alternative workaround to calling map:
调用 map 的另一种解决方法:
df['two'] = pd.Series(map_dict)
df
one two
A 10 every
B 20 good
C 30 boy
D 40 does
E 50 fine
In any case, until the mapping issue gets resolved (per juanpa.arrivillaga's comment) you have to convert either the index or the dict-to-map to a pandas Series.
无论如何,在映射问题得到解决之前(根据 juanpa.arrivillaga 的评论),您必须将索引或 dict-to-map 转换为熊猫系列。
回答by KenHBS
As of pandas version 0.23.x (released at May 15th, 2018) this problem is fixed:
从 pandas 0.23.x 版本(2018 年 5 月 15 日发布)开始,此问题已修复:
import pandas as pd
pd.__version__ # 0.23.4
df = pd.DataFrame({'one': {'A': 10, 'B': 20, 'C': 30, 'D': 40, 'E': 50}})
map_dict = {'A': 'every', 'B': 'good', 'C': 'boy', 'D': 'does', 'E': 'fine'}
df
# one
# A 10
# B 20
# C 30
# D 40
# E 50
df.index.map(map_dict)
# one
# every 10
# good 20
# boy 30
# does 40
# fine 50
From the What's New pagefor pandas 0.23.0 it says:
从Pandas 0.23.0的新增内容页面中,它说:
Index.map() can now accept Series and dictionary input objects (GH12756, GH18482, GH18509).
Index.map() 现在可以接受系列和字典输入对象(GH12756、GH18482、GH18509)。
For more information, check the help page of Index.map
有关更多信息,请查看Index.map的帮助页面
回答by tozCSS
A shorter alternative --with no explicit call to to_seriesor pd.Series:
更短的替代方案——没有显式调用to_seriesor pd.Series:
df['two'] = df.rename(map_dict).index
回答by JacobIRR
map(a python keyword) is apparently being used as a method of df.index
map(一个python关键字)显然被用作一种方法 df.index
Because this has its own internal demands, passing it an argument which has no __call__method is not allowed.
因为这有它自己的内部需求,所以__call__不允许给它传递一个没有方法的参数。
lambdaand functions are callable, a simple test:
lambda和函数是可调用的,一个简单的测试:
def foo():
pass
if foo.__call__:
print True
# Prints True
bar = lambda x: x+1
if bar.__call__:
print True
# Prints True
print {'1':'one'}.__call__
# AttributeError: 'dict' object has no attribute '__call__'

