如何修复列表索引超出范围的问题?
How can I fix the list index out of range problem?
所以我想用加入日期后一个月的日期填充发薪日期的 NaN 值。
Join date
Payday1
Okt'10
NaN
Des'10
NaN
我的期望输出是:
Join date
Payday1
Okt'10
Nov'10
Des'10
Jan'11
我试试这个代码:
months = ["Jan", "Feb", "Mar", "Apr","Mei","Jun","Jul","Agt","Sep","Okt","Nov","Des"]
dateIn="Okt'10"
def fill_date():
dateIn=dateIn.split("'")
month, year= dateIn[0], int(dateIn[1])
if month == months[len(months)-1]:
year+=1
month=months[0]
else:
for m in months:
if m == month:
month=months[months.index(month)+1]
dateOut=f"{month} {year}"
df['Payday1'] = df['Payday1'].apply(fill_date)
这个代码在这个代码month=months[months.index(month)+1]中是错误的,它说list index out of range
。那么如何修复此代码?
尝试:
def fill_date(dt):
mapper = {m: i+1 for i, m in enumerate(months)}
month, year = dt.split("'")
if mapper[month]==12:
return f"Jan'{int(year)+1}"
else:
return f"{months[mapper[month]]}'{year}"
df["Payday1"] = df["Join date"].apply(fill_date)
输入 df:
df = pd.DataFrame({"Join date": ["Okt'10", "Des'10"]})
如果要将索引约束在0
和len(months)
之间,可以使用取模运算符%
.
month=months[(months.index(month) + 1) % len(months)]
所以你会得到以下内容:
def fill_date(dateIn):
dateIn = dateIn.split("'")
month, year = dateIn[0], int(dateIn[1])
year += 1 if month == months[-1] else 0
month = months[(months.index(month) + 1) % len(months)]
dateOut = f"{month}'{year}"
return dateOut
我看到两种方法:
第一个: 在列表 months
的末尾添加第二个 Jan
,这可能有效。
其次: 在 for
循环中使用 break
,当找到第一个匹配元素时退出它。
所有的问题都是因为在 if m == month
中你给 month
分配了新的值并且在接下来的循环中同一行 if m == month
与 month
中的不同值进行比较并且它可能与 Des
和 tor 比较,在 Des
之后得到 elemenet。但是如果你使用 break
那么它会在找到第一个匹配值后退出(并且它不会检查 if m == month
的新值)。
具有其他更改的完整工作代码 - 即。你在代码中忘记了 return dateOut
。
months = ["Jan", "Feb", "Mar", "Apr","Mei","Jun","Jul","Agt","Sep","Okt","Nov","Des"]
dateIn = "Okt'10"
import pandas as pd
df = pd.DataFrame({
'Join date': ["Okt'10", "Des'10"],
'Payday1': ["NaN", "NaN"],
})
def fill_date(dateIn):
print(dateIn)
dateIn = dateIn.split("'")
month, year = dateIn[0], int(dateIn[1])
if month == months[-1]:
year += 1
month = months[0]
else:
for m in months:
if m == month:
month = months[months.index(month)+1]
break # <--- HERE
dateOut = f"{month}'{year}"
return dateOut
df['Payday1'] = df['Join date'].apply(fill_date)
print(df)
结果:
Join date Payday1
0 Okt'10 Nov'10
1 Des'10 Jan'11
可能问题也可以解决 for m in months[:-1]:
从列表中跳过 Des
。
所以我想用加入日期后一个月的日期填充发薪日期的 NaN 值。
Join date | Payday1 |
---|---|
Okt'10 | NaN |
Des'10 | NaN |
我的期望输出是:
Join date | Payday1 |
---|---|
Okt'10 | Nov'10 |
Des'10 | Jan'11 |
我试试这个代码:
months = ["Jan", "Feb", "Mar", "Apr","Mei","Jun","Jul","Agt","Sep","Okt","Nov","Des"]
dateIn="Okt'10"
def fill_date():
dateIn=dateIn.split("'")
month, year= dateIn[0], int(dateIn[1])
if month == months[len(months)-1]:
year+=1
month=months[0]
else:
for m in months:
if m == month:
month=months[months.index(month)+1]
dateOut=f"{month} {year}"
df['Payday1'] = df['Payday1'].apply(fill_date)
这个代码在这个代码month=months[months.index(month)+1]中是错误的,它说list index out of range
。那么如何修复此代码?
尝试:
def fill_date(dt):
mapper = {m: i+1 for i, m in enumerate(months)}
month, year = dt.split("'")
if mapper[month]==12:
return f"Jan'{int(year)+1}"
else:
return f"{months[mapper[month]]}'{year}"
df["Payday1"] = df["Join date"].apply(fill_date)
输入 df:
df = pd.DataFrame({"Join date": ["Okt'10", "Des'10"]})
如果要将索引约束在0
和len(months)
之间,可以使用取模运算符%
.
month=months[(months.index(month) + 1) % len(months)]
所以你会得到以下内容:
def fill_date(dateIn):
dateIn = dateIn.split("'")
month, year = dateIn[0], int(dateIn[1])
year += 1 if month == months[-1] else 0
month = months[(months.index(month) + 1) % len(months)]
dateOut = f"{month}'{year}"
return dateOut
我看到两种方法:
第一个: 在列表 months
的末尾添加第二个 Jan
,这可能有效。
其次: 在 for
循环中使用 break
,当找到第一个匹配元素时退出它。
所有的问题都是因为在 if m == month
中你给 month
分配了新的值并且在接下来的循环中同一行 if m == month
与 month
中的不同值进行比较并且它可能与 Des
和 tor 比较,在 Des
之后得到 elemenet。但是如果你使用 break
那么它会在找到第一个匹配值后退出(并且它不会检查 if m == month
的新值)。
具有其他更改的完整工作代码 - 即。你在代码中忘记了 return dateOut
。
months = ["Jan", "Feb", "Mar", "Apr","Mei","Jun","Jul","Agt","Sep","Okt","Nov","Des"]
dateIn = "Okt'10"
import pandas as pd
df = pd.DataFrame({
'Join date': ["Okt'10", "Des'10"],
'Payday1': ["NaN", "NaN"],
})
def fill_date(dateIn):
print(dateIn)
dateIn = dateIn.split("'")
month, year = dateIn[0], int(dateIn[1])
if month == months[-1]:
year += 1
month = months[0]
else:
for m in months:
if m == month:
month = months[months.index(month)+1]
break # <--- HERE
dateOut = f"{month}'{year}"
return dateOut
df['Payday1'] = df['Join date'].apply(fill_date)
print(df)
结果:
Join date Payday1
0 Okt'10 Nov'10
1 Des'10 Jan'11
可能问题也可以解决 for m in months[:-1]:
从列表中跳过 Des
。