Python 3.x iPython DF模式验证
我试图识别DF列中的模式:在“干燥日”列中,连续出现4个零(表示雨天),然后出现1个(干燥日)。当识别出该图案时,应在“条纹”列中标记1。结果不一致:有正确的匹配(2012-10-23)和错误的匹配(2012-10-17)。出了什么问题Python 3.x iPython DF模式验证,python-3.x,dataframe,Python 3.x,Dataframe,我试图识别DF列中的模式:在“干燥日”列中,连续出现4个零(表示雨天),然后出现1个(干燥日)。当识别出该图案时,应在“条纹”列中标记1。结果不一致:有正确的匹配(2012-10-23)和错误的匹配(2012-10-17)。出了什么问题 for i in range(0,2641): if sum(X2.iloc[i:(i+4),4]) ==0 and X2.iloc[(i+5),4] ==1: X2.iloc[(i+5),16] = 1 我创建了一个测试数据框(
for i in range(0,2641):
if sum(X2.iloc[i:(i+4),4]) ==0 and X2.iloc[(i+5),4] ==1:
X2.iloc[(i+5),16] = 1
我创建了一个测试数据框(只包含必要的列和16行),并且我得到了预期的结果。 我还手动创建了一个“预期”列,将“实际”(条纹)列与之进行比较 我使用的是Python3.8.3 64位,运行在VSCode中
import pandas as pd
data = {'Date': ['2012-10-13', '2012-10-14', '2012-10-15', '2012-10-16', '2012-10-17', '2012-10-18','2012-10-19', '2012-10-20', '2012-10-21', '2012-10-22','2012-10-23','2012-10-24' , '2012-10-25','2012-10-26', '2012-10-27', '2012-10-28', '2012-10-29' ],
'dry day': [0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0],
'expected':['', '', '', '', '', 'none', 'none', 'none', 'none', 'none', '1', 'none', 'none', 'none', 'none', 'none', 'none']
}
X2 = pd.DataFrame (data, columns = ['Date','dry day', 'expected'])
X2["actual"] = ""
for i in range(0,12):
if sum(X2.iloc[i:(i+4),1]) ==0 and X2.iloc[(i+5),1] ==1:
print(str(X2.iloc[i+5,0]) + " : is Streaky" )
X2.iloc[(i+5),3] = 1
else:
X2.iloc[(i+5),3] = 'none'
print(X2)
我创建了一个测试数据框(只有必要的列和16行),并且我得到了预期的结果。 我还手动创建了一个“预期”列,将“实际”(条纹)列与之进行比较 我使用的是Python3.8.3 64位,运行在VSCode中
import pandas as pd
data = {'Date': ['2012-10-13', '2012-10-14', '2012-10-15', '2012-10-16', '2012-10-17', '2012-10-18','2012-10-19', '2012-10-20', '2012-10-21', '2012-10-22','2012-10-23','2012-10-24' , '2012-10-25','2012-10-26', '2012-10-27', '2012-10-28', '2012-10-29' ],
'dry day': [0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0],
'expected':['', '', '', '', '', 'none', 'none', 'none', 'none', 'none', '1', 'none', 'none', 'none', 'none', 'none', 'none']
}
X2 = pd.DataFrame (data, columns = ['Date','dry day', 'expected'])
X2["actual"] = ""
for i in range(0,12):
if sum(X2.iloc[i:(i+4),1]) ==0 and X2.iloc[(i+5),1] ==1:
print(str(X2.iloc[i+5,0]) + " : is Streaky" )
X2.iloc[(i+5),3] = 1
else:
X2.iloc[(i+5),3] = 'none'
print(X2)
我收到警告:“SettingWithCopyWarning:试图在数据帧切片的副本上设置值。”答案可能是:-因为我使用的是原始pd DF的子集/副本。感谢您检查代码/逻辑!我得到了警告:“SettingWithCopyWarning:试图在数据帧切片的副本上设置值。”答案可能是:-因为我使用的是原始pd DF的子集/副本。感谢您检查代码/逻辑!