本文介绍了Python pandas数据框警告,建议使用.loc代替?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想通过删除丢失的信息来操纵数据,并将所有字母设为小写。但对于小写转换,我收到此警告:

Hi I would like to manipulate the data by removing missing information and make all letters lower case. But for the lowercase conversion, I get this warning:

E:\Program Files Extra \Python27 \lib\site-packages \ pandas \ core \frame.py:1808:UserWarning:Boolean系列键将重新编制索引以匹配DataFrame索引。
DataFrame index。,UserWarning)
C:\Users\KubiK\Desktop\FamSeach_NameHandling.py:18:SettingWithCopyWarning:
正在尝试设置一个值从DataFrame复制切片。
尝试使用.loc [row_indexer,col_indexer] = value而不是

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()
C:\ Users \ KubiK \Desktop \FamSeach_NameHandling .py:19:SettingWithCopyWarning:
尝试在DataFrame的切片副本上设置一个值。
尝试使用.loc [row_indexer,col_indexer] = value而不是

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy frame3["name"] = frame3["name"].str.lower()C:\Users\KubiK\Desktop\FamSeach_NameHandling.py:19: 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 [ethnicity] = frame3 [ethnicity]。str.lower()

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy frame3["ethnicity"] = frame3["ethnicity"].str.lower()

import pandas as pd
from pandas import DataFrame

# Get csv file into data frame
data = pd.read_csv("C:\Users\KubiK\Desktop\OddNames_sampleData.csv")
frame = DataFrame(data)
frame.columns = ["name", "ethnicity"]
name = frame.name
ethnicity = frame.ethnicity

# Remove missing ethnicity data cases
index_missEthnic = frame.ethnicity.isnull()
index_missName = frame.name.isnull()
frame2 = frame[index_missEthnic != True]
frame3 = frame2[index_missName != True]

# Make all letters into lowercase
frame3["name"] = frame3["name"].str.lower()
frame3["ethnicity"] = frame3["ethnicity"].str.lower()

# Test outputs
print frame3

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

This warning doesn't seem to be fatal (at least for my small sample data), but how should I deal with this?

样本数据

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,如下所示:

When you set frame2/3, trying using .loc as follows:

frame2 = frame.loc[~index_missEthnic, :]
frame3 = frame2.loc[~index_missName, :]

我认为这样可以解决你的错误'重新看到:

I think this would fix the error you're seeing:

frame3.loc[:, "name"] = frame3.loc[:, "name"].str.lower()
frame3.loc[:, "ethnicity"] = frame3.loc[:, "ethnicity"].str.lower()

您也可以尝试以下方法,但它没有回答您的问题:

You can also try the following, although it doesn't answer your question:

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]

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

This converts any string in the column into lowercase, otherwise it leaves the value untouched.

这篇关于Python pandas数据框警告,建议使用.loc代替?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 17:43