【问题标题】:split JSON/list of dictionaries in the column in dataframe to new rows in python (Solved)将数据框中列中的 JSON/字典列表拆分为 python 中的新行(已解决)
【发布时间】:2021-09-21 09:52:10
【问题描述】:

我对 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":"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/...
Name: custbody_attachment_1, Length: 1000, dtype: object

所以,我不知道如何转换它以便创建新行

已解决:

  1. 必须在pyodbc中使用这行代码(添加一列)来摆脱sql语句中的空值,所以第二部分不会引发错误

    NVL(transaction.custbody_attachment_1, '[{"FL":"","NM":""}]') as "custbody_attachment_1"

  2. 那一行解决了问题

    df = lineitem

    df['custbody_attachment_1']=df['custbody_attachment_1'].apply(json.loads) df = df.explode('custbody_attachment_1') df = pd.concat([df.drop(['custbody_attachment_1'],axis=1), df['custbody_attachment_1'].apply(pd.Series)],axis =1)

【问题讨论】:

标签: python json object transform dtype


【解决方案1】:
df = df.explode('Formula_Text')
df = pd.concat([df.drop(['Formula_Text'], axis=1), df['Formula_Text'].apply(pd.Series)], axis=1)
print(df)

【讨论】:

  • 解决了问题df = lineitemdf['custbody_attachment_1']=df['custbody_attachment_1'].apply(json.loads)df = df.explode('custbody_attachment_1')df = pd.concat([df.drop(['custbody_attachment_1'], axis=1), df['custbody_attachment_1'].apply(pd.Series)], axis =1)print(df)
  • 哦,我忽略了它 JSON 并决定它已经转换为字典列表。而df是DataFrame的通用约定名,所以直接用了
猜你喜欢
  • 2021-08-31
  • 2022-08-19
  • 2015-06-26
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 2010-12-19
相关资源
最近更新 更多