将值映射到df(pandas)的正确方法

时间:2017-04-10 19:50:39

标签: python pandas mapping

嘿伙计我有一个我在性别方面分开的df,我尝试将值映射到基于字典的列,如下所示

males = speed_dating.loc[speed_dating['gender'] == 0]
females = speed_dating.loc[speed_dating['gender'] == 1]


race_dictionary = {1.0: "Black", 2.0: "European", 3.0:"Hispanic", 4.0: "Asian", 5.0:"Native American", 6.0:"Other"}


males['race'] = males['race'].map(race_dictionary)

但是当我这样做时会发生什么

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  males['race'] = males['race'].map(race_dictionary)

我不明白因为我试图在我通过第一行建立的数据帧上设置值?

有人可以解释一下吗?

0 个答案:

没有答案