python - for 循环内的 dataframe.replace()

标签 python pandas dataframe

我正在尝试替换 num 列中的值。对于 ab 中的每个字母,我都有一本字典。我只显示了下面两个(A 和 B)

data = pd.DataFrame( {'ab' : ['A','B','A','A','B'], 'num' : ['01','02','01','01','01']})

a_replacements = { 'num' : { '01' : 'funny', '02' : 'serious' }}
b_replacements = { 'num' : { '01' : 'beginning', '02' : 'end' }}

data[data.ab == 'A'] = data[data.ab == 'A'].replace(inplace=True, to_replace=a_replacements)

最后一行的分配工作正常。但是,当我尝试在 for 循环中使用它时,我必须将 num 中的值替换为 ab 中的 26 个不同字母,我面临以下问题:

for letter in data.ab.unique():
    data.loc[data.ab == letter] = data.replace(to_replace=letter.lower()+"_replacements")

我得到:

<小时/>
TypeError                                 Traceback (most recent call last)
<ipython-input-96-acd3197ceef4> in <module>()
      1 for letter in data.ab.unique():
      2     print(letter.lower()+"_replacements")
----> 3     data.loc[data.ab == letter] = data.replace(to_replace=letter.lower()+"_replacements")

/Users/alokshenoy/.pyenv/versions/miniconda3-latest/lib/python3.6/site-packages/pandas/core/generic.py in replace(self, to_replace, value, inplace, limit, regex, method, axis)
   3427             if isinstance(to_replace, (tuple, list)):
   3428                 return _single_replace(self, to_replace, method, inplace,
-> 3429                                        limit)
   3430 
   3431             if not is_dict_like(to_replace):

/Users/alokshenoy/.pyenv/versions/miniconda3-latest/lib/python3.6/site-packages/pandas/core/generic.py in _single_replace(self, to_replace, method, inplace, limit)
     70     if self.ndim != 1:
     71         raise TypeError('cannot replace {0} with method {1} on a {2}'
---> 72                         .format(to_replace, method, type(self).__name__))
     73 
     74     orig_dtype = self.dtype

TypeError: cannot replace ['a_replacements'] with method pad on a DataFrame

关于如何解决这个问题有什么想法吗?

最佳答案

您可以使用groupby使用 apply 替换由 zip 创建的所有字典的字典,将列 ab 中的名称列表映射到字典列表:

a_replacements = { 'num' : { '01' : 'funny', '02' : 'serious' }}
b_replacements = { 'num' : { '01' : 'beginning', '02' : 'end' }}
abnames = ['A','B']
L = [a_replacements, b_replacements]

replacements = dict(zip(abnames, L))
print (replacements)
{'A': {'num': {'01': 'funny', '02': 'serious'}}, 
'B': {'num': {'01': 'beginning', '02': 'end'}}}

df = data.groupby('ab').apply(lambda x: x.replace(replacements[x.name]))
print (df)
  ab        num
0  A      funny
1  B        end
2  A      funny
3  A      funny
4  B  beginning

关于python - for 循环内的 dataframe.replace(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42754382/

相关文章:

python - 当数据项中不存在元素时如何获取零值

php - 为什么要避免使用 LAMP 托管的 Python 的 CGI?

python - Pandas - 找到第一次出现

python - 减少for循环时间

python - pandas DataFrame - 如何对行进行分组和标记

python - Python wsgi 应用程序的 HA 部署

python - "PerformanceWarning: DataFrame is highly fragmented. This is usually the result of calling ` 框架.插入 ` many times, which has poor performance."

python - 缺少数据,在 Pandas 中插入行并用 NAN 填充

python - 识别因果模型的影响时的错误

Python:如何将 sklearn 函数与数据帧一起使用?