我正在嘗試從資料庫中獲取資料,將它們放入資料框中并將它們加載到 AWS S3。
對于包含 None 值的欄位,這些值將作為 None 加載到 S3。我希望 VARCHAR 欄位中的 None 為 NULL 或空白,而 INT 欄位中的 None 為 0 或空白。
connection = pyodbc.connect(conn)
sql = 'SELECT id, name from table_a'
df = pd.read_sql_query(sql, connection)
df=df.applymap(str)
csv_buffer = BytesIO()
s3 = boto3.resource('s3')
with gzip.GzipFile(mode='w', fileobj=csv_buffer) as zipped_file:
df.to_csv(TextIOWrapper(zipped_file, 'utf8'), index=False)
s3.Object(bucket[env],dest_filename).put(Body=csv_buffer.getvalue())
我在列印(df)時得到的輸出是 -
| ID | 姓名 |
|---|---|
| 1 | 沒有任何 |
| 沒有任何 | 約翰 |
我希望將資料框加載到 S3,如下所示 -
| ID | 姓名 |
|---|---|
| 1 | |
| 約翰 |
我該如何處理這種情況?
uj5u.com熱心網友回復:
要在python中簡單地將 None 的值更改為空值,您可以嘗試:
for col in df.columns:
df[col] = df[col].fillna(str())
int() 也可以代替 str() ,或者空引號: ''
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/325038.html
上一篇:在python中使用JSON
