我想檢查從數(shù)據(jù)幀的左上角到最右下角元素的數(shù)據(jù)幀中的所有數(shù)據(jù)是否完整(數(shù)據(jù)應(yīng)填充為矩形)。如果在數(shù)據(jù)主體之后有空白列或行,這很好(它會有這個)。好的和壞的數(shù)據(jù)幀示例如下:bad_dataframe = pd.DataFrame([[1,1,1,""],["","","",""],[1,"",1,""],["","","",""]])good_dataframe = pd.DataFrame([[1,1,1,""],[1,1,1,""],[1,1,1,""],[1,1,1,""],["","","",""]])我這樣做的方式如下def not_rectangle_data(DataFrame): """ This function will check if the data given to it is a "rectangle" """ #removes all rows and columns that contain only blanks reduced_dataframe = DataFrame[DataFrame != ""].dropna(how="all",axis = 1).dropna(how="all",axis = 0) #removes all rows and columns that contain any blanks super_reduced_dataframe = reduced_dataframe.dropna(how="any",axis = 1).dropna(how="any",axis = 0) #Check that dataframe is not empty and that no column or no rows are half empty if not reduced_dataframe.empty and \ super_reduced_dataframe.equals(reduced_dataframe): #Check that columns in remain data are still present if ((max(reduced_dataframe.index) + 1) == reduced_dataframe.shape[0]) and \ ((max(reduced_dataframe.columns) + 1) == reduced_dataframe.shape[1]): return True else: return False else: return False但是我覺得應(yīng)該有一種更簡潔的方法來做到這一點。
1 回答

嗶嗶one
TA貢獻1854條經(jīng)驗 獲得超8個贊
使用numpy:
import numpy as np
def check_rectangle(df):
non_zeros = np.nonzero(df.values)
arr = np.zeros(np.max(non_zeros, 1)+1)
np.add.at(arr, non_zeros, 1)
return np.alltrue(arr)
check_rectangle(good_dataframe)
# True
check_rectangle(bad_dataframe)
# False
np.nonzero獲取所有不為零的索引(''此處視為零)。
np.zeros(np.max(non_zeros, 1)+1)創(chuàng)建適合 的最小矩形non_zeros。
np.add.at添加1到所有非零位置。
最后,如果矩形被填充,則np.alltrue返回True,否則返回False。
添加回答
舉報
0/150
提交
取消