我是Python的新手,我試圖找到一個答案,但我所嘗試的一切似乎都沒有用。而且大部分的答案都是在整個資料為JSON格式時提供的
。通過PYODBC,我使用下面的代碼來檢索資料
。formula = ""select id, type, custbody_attachment_1 from transaction ""
lineitem = pd.read_sql_query(formula, cnxn)
它給了我類似以下的東西
------------- ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Internal_ID | Type | Formula_Text |
------------- ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 2895531 | Bill | |
------------- ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 3492009 | Bill | [{"FL":"https://.app.netsuite.com/core/media/media.nl?id=someLinkToTheFile0","NM":"someFileName0"] |
------------- ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3529162 | Bill | [{"FL"/span>:"5https://.app.netsuite.com/core/media/media.nl? id=someLinkToTheFile1","NM":"someFileName1"},{"FL":"https://. app.netsuite.com/core/media/media.nl?id=someLinkToTheFile2","NM":"someFileName2"}] |
------------- ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
我需要像這樣的輸出。(單元格中可能有2個以上的鏈接。)
------------- ------ --------------------------------------------------------------------- ---------------
| Internal_ID | Type | FL | NM !
------------- ------ --------------------------------------------------------------------- ---------------
| 2895531 | Bill | | |
------------- ------ --------------------------------------------------------------------- ---------------
| 3492009 | Bill | https://.app.netsuite.com/core/media/media.nl?id=someLinkToTheFile0 | someFileName0 !
------------- ------ --------------------------------------------------------------------- ---------------
| 3529162 | Bill | https://.app.netsuite.com/core/media/media.nl?id=someLinkToTheFile1 | someFileName1 !
------------- ------ --------------------------------------------------------------------- ---------------
| 3529162 | Bill | https://.app.netsuite.com/core/media/media.nl?id=someLinkToTheFile2 | someFileName2 |
------------- ------ --------------------------------------------------------------------- ---------------
我試著玩了一下JSON,但問題一個接一個(因為對我來說它似乎是JSON資料)。最后我運行了
print(lineitem['custbody_attachment_1'] )
并在Python控制臺中得到如下結果
999 [{"FL":"https://4811553.app.netsuite.com/core/...
名稱: custbody_attachment_1, 長度: 1000, dtype: object
所以,我不知道如何轉換這個,以便我可以創建新的行
uj5u.com熱心網友回復:df = df.explode('Formula_Text')
df = pd.concat([df.drop(['Formula_Text'], axis=1), df['Formula_Text'].apply(pd.Series) ] , axis=1)
print(df)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/329113.html
標籤:
