2 回答

TA貢獻1788條經驗 獲得超4個贊
我認為該字段在讀取 excel 時會自動解析為浮點數。之后我會更正它:
df['column_name'] = df['column_name'].astype(int)
如果您的列包含空值,則無法轉換為整數,因此您需要先填充空值:
df['column_name'] = df['column_name'].fillna(0).astype(int)
然后你可以連接和存儲你的方式

TA貢獻1816條經驗 獲得超4個贊
您的問題與 Spark 或 PySpark 無關。它與Pandas相關。
這是因為 Pandas 會自動解釋和推斷列的數據類型。由于您的列的所有值都是數字,Pandas 會將其視為float數據類型。
為了避免這種情況,pandas.ExcelFile.parse方法接受一個名為 的參數converters,您可以使用它通過以下方式告訴 Pandas 特定的列數據類型:
# if you want one specific column as string
df = pd.concat([filepath_pd.parse(name, converters={'column_name': str}) for name in names])
或者
# if you want all columns as string
# and you have multi sheets and they do not have same columns
# this merge all sheets into one dataframe
def get_converters(excel_file, sheet_name, dt_cols):
cols = excel_file.parse(sheet_name).columns
converters = {col: str for col in cols if col not in dt_cols}
for col in dt_cols:
converters[col] = pd.to_datetime
return converters
df = pd.concat([filepath_pd.parse(name, converters=get_converters(filepath_pd, name, ['date_column'])) for name in names]).reset_index(drop=True)
或者
# if you want all columns as string
# and all your sheets have same columns
cols = filepath_pd.parse().columns
dt_cols = ['date_column']
converters = {col: str for col in cols if col not in dt_cols}
for col in dt_cols:
converters[col] = pd.to_datetime
df = pd.concat([filepath_pd.parse(name, converters=converters) for name in names]).reset_index(drop=True)
添加回答
舉報