Python 如何将值放入与条件匹配的列名的每一行
如果列名称与每个列表中的年份匹配,我一直在尝试将0变为1。 最后一个Python 如何将值放入与条件匹配的列名的每一行,python,pandas,dataframe,Python,Pandas,Dataframe,如果列名称与每个列表中的年份匹配,我一直在尝试将0变为1。 最后一个if语句给出了一个错误 df +-----------+------------+------+------+------+------+------+------+------+------+------+------+ | start | end | 2012 | 2013 | 2014 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | +-----
if
语句给出了一个错误
df
+-----------+------------+------+------+------+------+------+------+------+------+------+------+
| start | end | 2012 | 2013 | 2014 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 |
+-----------+------------+------+------+------+------+------+------+------+------+------+------+
| 2017/1/29 | 2019/9/10 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2012/1/30 | 2015/9/11 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2019/1/31 | 2021/05/08 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2016/2/1 | 2017/9/13 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2017/2/2 | 2019/9/14 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2017/2/3 | 2021/05/08 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2020/2/4 | 2020/9/16 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+-----------+------------+------+------+------+------+------+------+------+------+------+------+
for index, row in df.iterrows():
s_year = int(row['start'][:4])
e_year = int(row['end'][:4])
l_years = []
l_years.append(s_year)
while s_year < e_year:
s_year += 1
l_years.append(s_year)
for i in l_years:
for column_name in df.columns.values:
if i == column_name:
df[index][column_name] = 1
让我们尝试跨行广播年份列以创建掩码,然后替换为1,其中掩码为True:
#确保类型正确
df['start']=pd.to_datetime(df['start']]
df['end']=pd.to_datetime(df['end']]
#抓取年份栏
年份_columns=df.columns[2:]
#以numpy数组形式获取年份列作为数字
c=年份\列。到\ numpy().astype(int)
#广播起始年与列(以整数表示)和结束年与年列(以整数表示)
m=((df['start'].dt.year.to_numpy()[:,None]或者您可以尝试:
df['start'] = pd.to_datetime(df['start'])
df['end'] = pd.to_datetime(df['end'])
def change(x):
k = range(x['start'].year,x['end'].year+1)
for col in x.index[2:]:
if int(col) in k:
x[col] = 1
return x
df = df.apply(change,1)
我不知道在这种情况下如何使用to_numpy()
。从现在起,我将记住,编写代码以确保类型正确,以免出现任何可能的类型错误。使其成为函数后更容易发现错误。apply()
对我来说是新事物。我最终发现了map和apply之间的区别。
start end 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
0 2017-01-29 2019-09-10 0 0 0 0 0 1 1 1 0 0
1 2012-01-30 2015-09-11 1 1 1 1 0 0 0 0 0 0
2 2019-01-31 2021-05-08 0 0 0 0 0 0 0 1 1 1
3 2016-02-01 2017-09-13 0 0 0 0 1 1 0 0 0 0
4 2017-02-02 2019-09-14 0 0 0 0 0 1 1 1 0 0
5 2017-02-03 2021-05-08 0 0 0 0 0 1 1 1 1 1
6 2020-02-04 2020-09-16 0 0 0 0 0 0 0 0 1 0
df['start'] = pd.to_datetime(df['start'])
df['end'] = pd.to_datetime(df['end'])
def change(x):
k = range(x['start'].year,x['end'].year+1)
for col in x.index[2:]:
if int(col) in k:
x[col] = 1
return x
df = df.apply(change,1)