python中列之间存在任何NaN值时如何处理脚本
我试图处理一个脚本,其中我试图查找列之间的月份,该脚本工作正常,但每当任何字段为空时,它都会给出一个错误 如果任何NaN值介于两者之间,它必须跳过并移动到下一行 如何解决此错误: 输入数据:python中列之间存在任何NaN值时如何处理脚本,python,pandas,Python,Pandas,我试图处理一个脚本,其中我试图查找列之间的月份,该脚本工作正常,但每当任何字段为空时,它都会给出一个错误 如果任何NaN值介于两者之间,它必须跳过并移动到下一行 如何解决此错误: 输入数据: Month1 Month2 Month_list Mar2020 Dec2020 Nov2020 Jan2021 NaN NaN Sep2020 Feb2021 Oct2020 Dec2020 NaN NaN Dec2020 Mar2021 预期产
Month1 Month2 Month_list
Mar2020 Dec2020
Nov2020 Jan2021
NaN NaN
Sep2020 Feb2021
Oct2020 Dec2020
NaN NaN
Dec2020 Mar2021
预期产出:
预期产量
Month1 Month2 Month_list
Mar2020 Sep2020 Mar2020,Apr2020,May2020,Jun2020,Jul2020,Aug2020,Sep2020
Nov2020 Jan2021 Nov2020,Dec2020,Jan2021
NaN NaN NaN
Sep2020 Feb2021 Sep2020,Oct2020,Nov2020,Dec2020,Jan2021,Feb2021
Oct2020 Dec2020 Oct2020,Nov2020,Dec2020
NaN NaN NaN
Dec2020 Mar2021 Dec2020,Jan2021,Feb2021,Mar2021
代码:
Error:ValueError:start和end都不能是NaT您需要排除带有NaN值的列,一种方法如下: pd.concat[df,df.dropna.applylambda x:get_date_listx,axis=1。to_frame'Months_List',axis=1 输出: 出[169]: 月1月2个月清单 0 2020年3月2020年12月2020年3月2020年4月2020年5月2020年6月2020年7月。。。 2020年11月1日2021年1月2020年11月2020年12月2021年1月 2楠楠楠楠楠 2020年9月3日2020年2月2日2020年9月2020年10月2020年11月2020年12月2021年1月。。。 2020年10月4日2020年12月2020年10月2020年11月2020年12月 5楠楠楠楠楠 2020年12月6日2020年3月2021年12月2020年1月2021年2月2021年3月 2020年12月7日2020年3月2021年12月2020年1月2021年2月2021年3月
您需要使用NaN vlaues排除列,一种方法如下: pd.concat[df,df.dropna.applylambda x:get_date_listx,axis=1。to_frame'Months_List',axis=1 输出: 出[169]: 月1月2个月清单 0 2020年3月2020年12月2020年3月2020年4月2020年5月2020年6月2020年7月。。。 2020年11月1日2021年1月2020年11月2020年12月2021年1月 2楠楠楠楠楠 2020年9月3日2020年2月2日2020年9月2020年10月2020年11月2020年12月2021年1月。。。 2020年10月4日2020年12月2020年10月2020年11月2020年12月 5楠楠楠楠楠 2020年12月6日2020年3月2021年12月2020年1月2021年2月2021年3月 2020年12月7日2020年3月2021年12月2020年1月2021年2月2021年3月 尝试: df[月份列表]=df.loc[ df[[Month1,Month2]].notna.allaxis=1[Month1,Month2] ].applylambda x:get_date_listx,axis=1 printdf 印刷品: 月1月2月清单 0 2020年3月2020年12月2020年3月2020年4月2020年5月2020年6月2020年7月。。。 2020年11月1日2021年1月2020年11月2020年12月2021年1月 2楠楠楠楠楠 2020年9月3日2020年2月2日2020年9月2020年10月2020年11月2020年12月2021年1月。。。 2020年10月4日2020年12月2020年10月2020年11月2020年12月 5楠楠楠楠楠 2020年12月6日2020年3月2021年12月2020年1月2021年2月2021年3月 尝试: df[月份列表]=df.loc[ df[[Month1,Month2]].notna.allaxis=1[Month1,Month2] ].applylambda x:get_date_listx,axis=1 printdf 印刷品: 月1月2月清单 0 2020年3月2020年12月2020年3月2020年4月2020年5月2020年6月2020年7月。。。 2020年11月1日2021年1月2020年11月2020年12月2021年1月 2楠楠楠楠楠 2020年9月3日2020年2月2日2020年9月2020年10月2020年11月2020年12月2021年1月。。。 2020年10月4日2020年12月2020年10月2020年11月2020年12月 5楠楠楠楠楠 2020年12月6日2020年3月2021年12月2020年1月2021年2月2021年3月
IIUC,您的函数需要if else块:
def get_date_list(x):
if not pd.isna(x['Month1']):
return ",".join(
item.strftime("%b %Y")
for item in pd.date_range(x['Month1'], x['Month2'], freq="MS")
)
return np.nan
df['Month_list'] = df.apply(lambda x: get_date_list(x), axis=1)
IIUC,您的函数需要if else块:
def get_date_list(x):
if not pd.isna(x['Month1']):
return ",".join(
item.strftime("%b %Y")
for item in pd.date_range(x['Month1'], x['Month2'], freq="MS")
)
return np.nan
df['Month_list'] = df.apply(lambda x: get_date_list(x), axis=1)
@ThePyGuy-不使用建议的用例。您可以用NaN值屏蔽行。@ThePyGuy-请给出一个答案,正如我尝试过的,但它不起作用。@ThePyGuy-不使用建议的用例。您可以用NaN值屏蔽行。@ThePyGuy-请给出答案,正如我已经尝试过的,但它不起作用。我想在上面的代码中添加这行代码。将最后一行替换为df=pd.concat[df,df.dropna.applylambda x:get_date_listx,axis=1。为了框定'Months_List',axis=1,我已经尝试过像这样使用df['Months_List']=pd.concat[df,df.dropna.applylambda x:get_date_listx,axis=1],axis=1,但是,它以日期格式给出输出,而不是月份列表。将结果分配回df,而不是df['Months\u list',],我想在上面的代码中添加这行代码。将最后一行替换为df=pd.concat[df,df.dropna.applylambda x:get_Date\u listx,axis=1。到框架'Months\u list',axis=1,我已经尝试过像这样使用df['Months\u list']=pd.concat[df,df.dropna.applylambda x:get_date_listx,axis=1],axis=1,但它以日期格式给出输出。将结果分配回df而不是df['Months_list']
print(df)
Month1 Month2 Month_list
0 Mar2020 Dec2020 Mar 2020,Apr 2020,May 2020,Jun 2020,Jul 2020,A...
1 Nov2020 Jan2021 Nov 2020,Dec 2020,Jan 2021
2 NaN NaN NaN
3 Sep2020 Feb2021 Sep 2020,Oct 2020,Nov 2020,Dec 2020,Jan 2021,F...
4 Oct2020 Dec2020 Oct 2020,Nov 2020,Dec 2020
5 NaN NaN NaN
6 Dec2020 Mar2021 Dec 2020,Jan 2021,Feb 2021,Mar 2021