Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 2.7 Python数据帧警告,建议改用.loc?_Python 2.7_Indexing_Pandas - Fatal编程技术网

Python 2.7 Python数据帧警告,建议改用.loc?

Python 2.7 Python数据帧警告,建议改用.loc?,python-2.7,indexing,pandas,Python 2.7,Indexing,Pandas,嗨,我想通过删除丢失的信息并使所有字母小写来处理数据。但对于小写转换,我得到以下警告: E:\Program Files Extra\Python27\lib\site-packages\pandas\core\frame.py:1808: UserWarning: Boolean Series key will be reindexed to match DataFrame index. "DataFrame index.", UserWarning) C:\Users\KubiK\Des

嗨,我想通过删除丢失的信息并使所有字母小写来处理数据。但对于小写转换,我得到以下警告:

E:\Program Files Extra\Python27\lib\site-packages\pandas\core\frame.py:1808: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  "DataFrame index.", UserWarning)
C:\Users\KubiK\Desktop\FamSeach_NameHandling.py:18: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
请参阅文档中的注意事项:
frame3[“name”]=frame3[“name”].str.lower()

请参阅文档中的注意事项:
frame3[“种族”]=frame3[“种族”].str.lower()

这个警告似乎不是致命的(至少对于我的小样本数据而言),但我应该如何处理呢

样本数据

Name    Ethnicity
Thos C. Martin                              Russian
Charlotte Wing                              English
Frederick A T Byrne                         Canadian
J George Christe                            French
Mary R O'brien                              English
Marie A Savoie-dit Dugas                    English
J-b'te Letourneau                           Scotish
Jane Mc-earthar                             French
Amabil?? Bonneau                            English
Emma Lef??c                                 French
C., Akeefe                                  African
D, James Matheson                           English
Marie An: Thomas                            English
Susan Rrumb;u                               English
                                            English
Kaio Chan   

设置frame2/3时,请尝试使用.loc,如下所示:

frame2 = frame.loc[~index_missEthnic, :]
frame3 = frame2.loc[~index_missName, :]
我认为这将修复您看到的错误:

frame3.loc[:, "name"] = frame3.loc[:, "name"].str.lower()
frame3.loc[:, "ethnicity"] = frame3.loc[:, "ethnicity"].str.lower()
您也可以尝试以下方法,尽管它不能回答您的问题:

frame3.loc[:, "name"] = [t.lower() if isinstance(t, str) else t for t in frame3.name]
frame3.loc[:, "ethnicity"] = [t.lower() if isinstance(t, str) else t for t in frame3. ethnicity]

这会将列中的任何字符串转换为小写,否则它会保持值不变。

不确定为什么需要这么多布尔值。。。 还要注意,
.isnull()
不会捕获空字符串。 在应用
.lower()
之前过滤空字符串似乎也不是必需的。 但这是有必要的。。。这对我很有用:

frame = pd.DataFrame({'name':['Abc Def', 'EFG GH', ''], 'ethnicity':['Ethnicity1','', 'Ethnicity2']})
print frame

    ethnicity     name
0  Ethnicity1  Abc Def
1               EFG GH
2  Ethnicity2         

name_null = frame.name.str.len() == 0
frame.loc[~name_null, 'name'] = frame.loc[~name_null, 'name'].str.lower()
print frame

    ethnicity     name
0  Ethnicity1  abc def
1               efg gh
2  Ethnicity2         

谢谢,我按照你的建议做了,但是仍然有如下警告消息:\Users\KubiK\Desktop\FamSeach\u nameholling.py:18:SettingWithCopyWarning:试图在数据帧切片的副本上设置值。尝试使用.loc[row\u indexer,col\u indexer]=value,请参见文档中的注意事项:frame3[“name”]=frame3[“name”].str.lower()C:\Users\KubiK\Desktop\FamSeach\u nameholling.py:19:SettingWithCopyWarning:试图在数据帧的切片副本上设置值。请尝试使用.loc[row\u indexer,col\u indexer]=value代替。请参阅文档中的注意事项:frame3[“种族”]=frame3[“种族”].str.lower()@KubiK888:BTW,它有助于提供一些示例数据,以便用户可以使用它(相对于计算机上的csv文件)。@Alexander您仍在分配副本。我想你应该编辑你的答案,以反映我上面的评论,这样未来的读者就不必浏览所有这些评论了。我很好奇你是否阅读了警告建议的注意事项。他们怎么没能帮你回答这个问题?我确实读过警告。这是一个过于简化的示例,对于像我这样的新程序员来说是没有用的。即使当我遵循有经验的程序员关于使用.loc的建议时,同样的警告仍然存在(如下所示)。在我看来,这个例子与您的非常相似。在下面的答案中,您仍然在分配副本--
loc
In,换句话说,它位于错误的一侧。见我对那篇文章的评论。
frame3.loc[:, "name"] = [t.lower() if isinstance(t, str) else t for t in frame3.name]
frame3.loc[:, "ethnicity"] = [t.lower() if isinstance(t, str) else t for t in frame3. ethnicity]
frame = pd.DataFrame({'name':['Abc Def', 'EFG GH', ''], 'ethnicity':['Ethnicity1','', 'Ethnicity2']})
print frame

    ethnicity     name
0  Ethnicity1  Abc Def
1               EFG GH
2  Ethnicity2         

name_null = frame.name.str.len() == 0
frame.loc[~name_null, 'name'] = frame.loc[~name_null, 'name'].str.lower()
print frame

    ethnicity     name
0  Ethnicity1  abc def
1               efg gh
2  Ethnicity2