Python 检查dataframe列是否已填充并按字符串搜索
我有以下数据帧:Python 检查dataframe列是否已填充并按字符串搜索,python,dataframe,notnull,Python,Dataframe,Notnull,我有以下数据帧: import pandas as pd import re df = pd.DataFrame({'Column_01': ['Press', 'Temp', '', 'Strain gauge', 'Ultrassonic', ''], 'Column_02': ['five', 'two', 'five', 'five', 'three', 'three']}) 首先,我想检查“第0
import pandas as pd
import re
df = pd.DataFrame({'Column_01': ['Press', 'Temp', '', 'Strain gauge', 'Ultrassonic', ''],
'Column_02': ['five', 'two', 'five', 'five', 'three', 'three']})
首先,我想检查“第01列”是否已填写。
如果填写了“Columns_01”或“Column_02”包含单词“一”、“二”、“三”。新的列分类器将接收“传感器”
为了识别“Column_02”字符串,我实现了以下代码:
df['Classifier'] = df.apply(lambda x: 'SENSOR'
if re.search(r'one|two|three', x['Column_02'])
else 'Nan', axis = 1)
此代码正在运行。它完全可以在数据帧行上找到字符串。但是,我还需要检查“Column_01”是否已填充。我无法使用notnull函数来解决这个问题
我希望输出为:
Column_01 Column_02 Classifier
Press five SENSOR #current line of Column_01 completed
Temp two SENSOR #current line of Column_02 completed; string 'two'
five Nan
Strain gauge five SENSOR #current line of Column_01 completed
Ultrassonic three SENSOR #current line of Column_01 completed; string 'three'
three SENSOR #string 'three'
一般来说,你应该避免。应用ref 这应该可以做到: 将numpy作为np导入 df[Classifier]=np.wheredf[Column_01]。fillna.ne | df[Column_02]。str.containsone | two | two,SENSOR,np.nan 产出: 列\u 01列\u 02分类器 0按5传感器 1温度2传感器 二五南 3应变计五传感器 4超声波三传感器 5三传感器