Я пытаюсь определить «абсолютное значение» числа, а затем преобразовать его в строка с точностью до 2 десятичных знаков
Есть идеи, где я ошибаюсь?
Код: Выделить всё
def extract_data(filename, sheet_name):
# DataFrame containing data extracted from Excel sheet
df = pd.read_excel(filename, sheet_name=sheet_name, skiprows=[0])
# Drop rows and columns with all NaN values
df.dropna(how='all', axis=0, inplace=True)
df.dropna(how='all', axis=1, inplace=True)
# Format columns based on their data types
for col in df.columns:
if df[col].dtype == 'object': #Handle text or categorical data
df[col] = df[col].astype(str).str.strip() #Remove leading/trailing whitespace
elif df[col].dtype == 'float64': #Format numeric data
# Format specific columns based on required use case
if col == "Total Cost": #Selecting column based on header value
df[col] = df[col].apply(lambda x: '{:.2f}'.format(abs(x)) if pd.notnull(x) else "")
Подробнее здесь: https://stackoverflow.com/questions/789 ... ws-as-0-50