使用pandas时遇到‘ValueError: cannot set a frame with no defined index and a scalar’错误,其中一个原因可能是由于你正在对一个空的dataframe进行操作

>>> import pandas as pd
>>> df = pd.DataFrame()
>>> df.loc[:, 'test'] = 1
ValueError: cannot set a frame with no defined index and a scalar

在进行此类操作时要通过df.empty或者len(df)进行判断是否为空再进行操作

if not df.empty:
	df.loc[:, 'test'] = 1

参考:https://stackoverflow.com/a/57327353/7151777

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐