Python 基于中的其他两列比较更改列的值
对于在pandas中创建的以下数据表Python 基于中的其他两列比较更改列的值,python,pandas,indexing,time-series,Python,Pandas,Indexing,Time Series,对于在pandas中创建的以下数据表 Date Score Study_Date 02/2011 70 11/2012 03/2011 72 11/2012 10/2011 60 11/2012 12/2011 50 11/2012 01/2012 40 11/2012 02/2012 60 11/2012 03/2012
Date Score Study_Date
02/2011 70 11/2012
03/2011 72 11/2012
10/2011 60 11/2012
12/2011 50 11/2012
01/2012 40 11/2012
02/2012 60 11/2012
03/2012 75 11/2012
11/2012 70 11/2012
12/2012 70 11/2012
01/2013 30 11/2012
02/2013 20 11/2012
04/2013 60 11/2012
06/2013 80 11/2012
我想将日期早于研究日期的行的所有分数替换为0
我尝试了以下方法:
df[df.Date < df.Study_Date, 'Score']=0
df[df.Date
但我得到:
TypeError:“Series”对象是可变的,因此无法对其进行散列
任何帮助都将不胜感激。用于分配值,将感兴趣的列命名为第二个“参数”。这里的前两行只是为了确保您的日期列是正确的datetime
系列(如果它们已经是正确的dtype
,您可以跳过它们)
用于指定值,将感兴趣的列命名为第二个“参数”。这里的前两行只是为了确保您的日期列是正确的datetime
系列(如果它们已经是正确的dtype
,您可以跳过它们)
# Make sure your columns are datetimes:
df['Date'] = pd.to_datetime(df.Date)
df['Study_Date'] = pd.to_datetime(df.Study_Date)
# Use .loc
df.loc[df.Date < df.Study_Date, 'Score'] = 0
Date Score Study_Date
0 2011-02-01 0 2012-11-01
1 2011-03-01 0 2012-11-01
2 2011-10-01 0 2012-11-01
3 2011-12-01 0 2012-11-01
4 2012-01-01 0 2012-11-01
5 2012-02-01 0 2012-11-01
6 2012-03-01 0 2012-11-01
7 2012-11-01 70 2012-11-01
8 2012-12-01 70 2012-11-01
9 2013-01-01 30 2012-11-01
10 2013-02-01 20 2012-11-01
11 2013-04-01 60 2012-11-01
12 2013-06-01 80 2012-11-01
df['Score'][df.Date < df.Study_Date] = 0