Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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_String_Pandas - Fatal编程技术网

Python 将列按部分字符串匹配大小分配给数组维度错误

Python 将列按部分字符串匹配大小分配给数组维度错误,python,string,pandas,Python,String,Pandas,我有这样一个数据帧: Postcode Country 0 PR2 6AS United Kingdom 1 PR2 6AS United Kingdom 2 CF5 3EG United Kingdom 3 DG2 9FH United Kingdom 我根据部分字符串匹配创建要分配的新列: mytestdf['In_Preston'] = "FALSE" mytestdf Postcode Country In_Preston 0

我有这样一个数据帧:

  Postcode         Country
0  PR2 6AS  United Kingdom
1  PR2 6AS  United Kingdom
2  CF5 3EG  United Kingdom
3  DG2 9FH  United Kingdom
我根据部分字符串匹配创建要分配的新列:

mytestdf['In_Preston'] = "FALSE"

mytestdf

  Postcode         Country In_Preston
0  PR2 6AS  United Kingdom      FALSE
1  PR2 6AS  United Kingdom      FALSE
2  CF5 3EG  United Kingdom      FALSE
3  DG2 9FH  United Kingdom      FALSE
我希望通过“Postcode”上的部分字符串匹配来指定“In_Preston”列。我尝试以下方法:

mytestdf.loc[(mytestdf[mytestdf['Postcode'].str.contains("PR2")]), 'In_Preston'] = "TRUE"
但这将返回错误“无法将大小为3的序列复制到维度为2的数组轴”

我再次查看我的代码,并相信问题在于我正在从数据帧的片段中选择数据帧的片段。因此,我改为

mytestdf.loc[(mytestdf['Postcode'].str.contains("PR2")]), 'In_Preston'] = "TRUE"
但是我的解释器告诉我这是不正确的语法,尽管我不明白为什么


我的代码或方法中有什么错误?

您需要删除内部过滤器:

mytestdf.loc[mytestdf['Postcode'].str.contains("PR2"), 'In_Preston'] = "TRUE"
另一个解决方案是使用:

但如果要指定布尔值
True
s和
False
s:

mytestdf['In_Preston'] = mytestdf['Postcode'].str.contains("PR2")
print (mytestdf)
  Postcode         Country  In_Preston
0  PR2 6AS  United Kingdom        True
1  PR2 6AS  United Kingdom        True
2  CF5 3EG  United Kingdom       False
3  DG2 9FH  United Kingdom       False
编辑人:

如果需要,只检查邮政编码的开头:

mytestdf.Postcode.str.startswith('PR2')
或者为字符串的开头添加regex
^

mytestdf['Postcode'].str.contains("^PR2")

mytestdf.Postcode.str.startswith('PR2')
将是更合适的答案,同时还有许多改进的备选方案。反应非常好,非常感谢您的帮助。
mytestdf['Postcode'].str.contains("^PR2")