Python 3.x 如何将数据帧中的多个数字列同时转换为类别?

Python 3.x 如何将数据帧中的多个数字列同时转换为类别?,python-3.x,plotly-dash,Python 3.x,Plotly Dash,我有一个数字介于1和7之间的df数据帧,我想将它们转换为类别(negif 5和neutif not): 所以我试着: for col in columns: for _, row in df[col].iterrows(): if row<3: df.iloc[i,row] = "neg" elif row >5: df.iloc[i,row] = "pos" else:

我有一个数字介于1和7之间的
df
数据帧,我想将它们转换为类别(
neg
if 5和
neut
if not):

所以我试着:

for col in columns:
    for _, row in df[col].iterrows():
        if row<3:
            df.iloc[i,row] = "neg"
        elif row >5:
            df.iloc[i,row] = "pos"
        else:
            df.iloc[i,row] = "neut"
但它返回:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ('Openess', 'Conscientiousness', 'Extraversion', 'Agreeableness', 'Neuroticism')

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-132-bd0104cc9380> in <module>
     11 #             df.iloc[i,row] = "neut"
     12 
---> 13 df.loc[df['Openess','Conscientiousness', 'Extraversion', 'Agreeableness','Neuroticism'] > 5, ['Openess','Conscientiousness', 'Extraversion', 'Agreeableness','Neuroticism']] = "pos"

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ('Openess', 'Conscientiousness', 'Extraversion', 'Agreeableness', 'Neuroticism')
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
C:\ProgramData\Anaconda3\lib\site packages\pandas\core\index\base.py in get\u loc(self、key、method、tolerance)
2645请尝试:
->2646返回自引擎。获取位置(钥匙)
2647键错误除外:
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
关键错误:(‘开放’、‘认真’、‘外向’、‘宜人’、‘神经质’)
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
在里面
11#df.iloc[i,row]=“neut”
12
--->13 df.loc[df[‘开放’、‘认真’、‘外向’、‘宜人’、‘神经质’]>5、[‘开放’、‘认真’、‘外向’、‘宜人’、‘神经质’]=“pos”
C:\ProgramData\Anaconda3\lib\site packages\pandas\core\frame.py in\uuuuu getitem\uuuuuuuu(self,key)
2798如果self.columns.nlevels>1:
2799返回自我。\u获取项目\u多级(键)
->2800索引器=self.columns.get_loc(键)
2801如果是_整数(索引器):
2802索引器=[索引器]
C:\ProgramData\Anaconda3\lib\site packages\pandas\core\index\base.py in get\u loc(self、key、method、tolerance)
2646返回自引擎。获取位置(钥匙)
2647键错误除外:
->2648返回self.\u引擎。获取self.\u loc(self.\u可能\u cast\u索引器(键))
2649 indexer=self.get\u indexer([key],method=method,tolerance=tolerance)
2650如果indexer.ndim>1或indexer.size>1:
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
关键错误:(‘开放’、‘认真’、‘外向’、‘宜人’、‘神经质’)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-124-71393cdf0d1e> in <module>
      2 
      3 for col in columns:
----> 4     for _, row in df[col].iterrows():
      5         if row<3:
      6             df.iloc[i,row] = "neg"

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   5272             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5273                 return self[name]
-> 5274             return object.__getattribute__(self, name)
   5275 
   5276     def __setattr__(self, name: str, value) -> None:

AttributeError: 'Series' object has no attribute 'iterrows'
df.loc[df['A','B', 'C', 'D','E'] > 5, ['A','B', 'C', 'D','E']] = "pos"
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ('Openess', 'Conscientiousness', 'Extraversion', 'Agreeableness', 'Neuroticism')

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-132-bd0104cc9380> in <module>
     11 #             df.iloc[i,row] = "neut"
     12 
---> 13 df.loc[df['Openess','Conscientiousness', 'Extraversion', 'Agreeableness','Neuroticism'] > 5, ['Openess','Conscientiousness', 'Extraversion', 'Agreeableness','Neuroticism']] = "pos"

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ('Openess', 'Conscientiousness', 'Extraversion', 'Agreeableness', 'Neuroticism')