好友
阅读权限35
听众
最后登录1970-1-1
|
ymhld
发表于 2020-3-18 21:25
本帖最后由 ymhld 于 2020-4-5 12:52 编辑
[Python] 纯文本查看 复制代码 import os
import pandas as pd
chengji=[['优',95,100,99],['良',98,99,100],['可',95,98,88],['可',98,95,87],['劣',85,96,85],['优',94,93,91]]
data=pd.DataFrame(chengji,columns=['语文','类别','数学','政治'])
data1=data.copy(deep=True)
print("-"*50,'replace方法,只替换100')
data1['数学'] = data1['数学'].replace(100,"优+")
print(data1)
print("-"*50,'replace方法,替换96-100')
data1['数学'] = data1['数学'].replace([100,99,98,97,96],"优")
print(data1)
print("-"*50,'replace方法,替换小于95')
data1=data.copy(deep=True)
data1.loc[:,'数学'][data1['数学']<=95]="良"
print(data1)
test.py:18: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas- ... -view-versus-a-copy
data1.loc[:,'数学'][data1['数学']<=95]="良"
总有这样的提示信息,应该怎么写才能对呢,
解决方案:
data1.loc[data1['数学']<=95,'数学'] = "良" |
高级解决方案
[Python] 纯文本查看 复制代码 chengji = [['优', 95, 100, 99], ['良', 98, 99, 100], ['可', 95, 59, 88], ['可', 98, 95, 87], ['劣', 85,61, 85],
['优', 94, 93, 91]]
data = pd.DataFrame(chengji, columns=['语文', '类别', '数学', '政治'])
data1 = data.copy(deep=True)
print(data1)
data1['数学'] = data1['数学'].apply(lambda x: "优+" if x >= 100
else "优" if 100>x >= 95
else "良" if 95>x >= 85
else "及格" if 85>x >= 60
else "不及格" if 60>x
else x) |
|