У меня проблема с отображением имени Dataframe и проверкой строки и столбца в Dataframe в Python.
Вот мои кадры данных со строками и столбцами.
print("x_train: ",x_train.shape)
print("x_test: ",x_test.shape)
print("y_train: ",y_train.shape)
print("y_test: ",y_test.shape)
Вывод показано ниже.
x_train: (30, 455)
x_test: (30, 114)
y_train: (455,)
y_test: (114,)
Я написал фрагмент кода, показанный ниже, чтобы показать имя Dataframe, а также строки и столбцы Dataframe. Выдает tuple out of range
, потому что y_train
и y_test
не имеют столбца.
def showRowsandColumns(value):
name =[x for x in globals() if globals()[x] is value][0]
if not isinstance(value, pd.DataFrame):
value = value.to_frame()
if not value.shape[0] and value.shape[1]:
value_count_row = value.shape[0] # gives number of row count
value_count_col = value.shape[1] # gives number of col count
elif value.shape[0] and not value.shape[1]:
value_count_row = value.shape[0] # gives number of row count
value_count_col = 0
elif not value.shape[0] and value.shape[1]:
value_count_row = 0 # gives number of row count
value_count_col = value.shape[1]
else:
value_count_row = value.shape[0] # gives number of row count
value_count_col = value.shape[1] # gives number of col count
print("{} : {} rows and {} columns "
.format(name,value_count_row,value_count_col))
showRowsandColumns(x_train)
showRowsandColumns(x_test)
showRowsandColumns(y_train)
showRowsandColumns(y_test)
ошибка:
AttributeError: 'numpy.ndarray' object has no attribute 'to_frame'
Как я могу это исправить?