python的pandas用loc清除数据_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 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

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

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?

Sample data

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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值