Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 值在超过字符数时更改_Python_Pandas - Fatal编程技术网

Python 值在超过字符数时更改

Python 值在超过字符数时更改,python,pandas,Python,Pandas,我试图找出当列中的值超过字符数时,它将更改另一列中的值。例如,在“Body”列中,每当csv中的行超过字符数156时,它将更改另一列名称“Double”中的值1 数据帧示例: Body Double Hello John, What is your name again? Can you check 0 it now for

我试图找出当列中的值超过字符数时,它将更改另一列中的值。例如,在“Body”列中,每当csv中的行超过字符数156时,它将更改另一列名称“Double”中的值1

数据帧示例:

                       Body                                           Double
Hello John, What is your name again? Can you check                       0
it now for the exam later?                                               
Dear Mr Sammy, I've found out about the exam that Mr                     0
Wick failed on last semester, would you think of 
any recommendation?
上面是我的csv文件的一个示例。在“Body”列中,我在“Body”列中设置了在哪个字符上有
.str.len(156)
。如果我将“RM0”放在文本前面,并且它超过了字符数,它将在“Double”列中赋值1。(假设正文列中包含156个字符)

下面是我目前的代码

 for p5 in df:
        if df[(df['BDE'] == '0') & (df['ADD'].isull())]:
           df.loc[df['Body'].gt(156), 'Double'] = '1'
“BDE”和“ADD”只是为了过滤列目的

我的期望输出:

                        Body                                     Double
RM0 Hello John, What is your name again? Can you check             1
it now for the exam later?  
我对pandas很陌生,对python的掌握还不到一个月,因此,如果您能强调我的错误并推荐任何建议以实现我所期望的输出,我将不胜感激

**尝试以下操作后出现错误:**

df.loc[(df['BDE'] == '0') & (df['ADD'].isull()) & df['Body'].str.len().gt(156), 'Double'] = '1'
回溯:

    During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/syafiq/opt/anaconda3/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/Users/syafiq/Downloads/RoutingPractice01.py", line 80, in main
    df.loc[(df['BDE'] == '0') & (df['ADD'].isull()) & df['Body'].str.len().gt(156), 'Double'] = '1'
  File "/Users/syafiq/opt/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py", line 2980, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/Users/syafiq/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2899, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'BDE'

非常感谢:)

我认为您需要为按位和:

df.loc[(df['BDE'] == '0') & (df['ADD'].isull()) & df['Body'].gt(156), 'Double'] = '1'
或:


您好@jezrael,我没有意识到可以将.str.len().gt放在一起,我已经尝试过了,但是,在处理上述异常时,它给了我一个错误,出现了另一个异常:@SyafiqRosli-
的意思是“BDE”和“ADD”只是过滤列的问题?由于错误意味着没有列
BDE
ADD
,您需要一些不同的东西吗?BDE和ADD只是为了过滤列,有一个列名“BDE”和“ADD”@SyafiqRosli-hmmm,
键错误:“BDE”
意味着没有列
BDE
,什么是
打印(df.columns.tolist())
?在你提到它之后,现在我意识到了原因。因为这行代码是在我完成连接任务和按列分组之后生成的,当前df只包含BDE不在当前df中的特定列。
df.loc[(df['BDE'] == '0') & (df['ADD'].isull()) & df['Body'].str.len().gt(156), 'Double'] = '1'