【问题标题】:Pandas to_sql index with MultiIndex columns带有 MultiIndex 列的 Pandas to_sql 索引
【发布时间】:2017-10-12 05:22:29
【问题描述】:

我正在尝试将具有 MultiIndex 列的 DataFrame 写入 MS SQL 数据库。索引将输出为 NULL。如果我只有单列,它可以正常工作。

l1 = ['foo', 'bar']
l2 = ['a', 'b', 'c']
cols = pd.MultiIndex.from_product([l1, l2])
df = pd.DataFrame(np.random.random((3,6)), index=[1,2,3], columns=cols)
df.to_sql('test', conn, if_exists='replace')

How it looks in SQL

这是一个错误还是我需要做其他事情才能正确编写索引?

【问题讨论】:

  • df.to_sql('test', conn, if_exists='replace', index=False) 做你想做的事吗?
  • 不,那只是在没有索引的情况下编写它。
  • 有解决办法吗?我也有同样的问题。

标签: python sql sql-server pandas


【解决方案1】:

您可以连接数据框的第一级:

l1 = ['foo', 'bar']
l2 = ['a', 'b', 'c']
cols = pd.MultiIndex.from_product([l1, l2])
df = pd.DataFrame(np.random.random((3,6)), index=[1,2,3], columns=cols)
pd.concat([df['foo'],df['bar']]).to_sql('test', conn, if_exists='replace')

这导致了这个表:

index                a                      b                      c
-------------------- ---------------------- ---------------------- ----------------------
1                    0.803555407060559      0.0185295254735488     0.702949767792433
2                    0.257823384796912      0.985716269729717      0.749719964181681
3                    0.909115063376081      0.236242172285058      0.932813789580215
1                    0.898527697819921      0.874431627680823      0.805393798630385
2                    0.97537971906356       0.319221893730643      0.584449093938984
3                    0.678625747581189      0.606321574437647      0.437746301372623

如果您想要更接近您链接到的 SQL 表示例,您可以使用合并并为每一列添加后缀:

l1 = ['foo', 'bar']
l2 = ['a', 'b', 'c']
cols = pd.MultiIndex.from_product([l1, l2])
df = pd.DataFrame(np.random.random((3,6)), index=[1,2,3], columns=cols)
pd.merge(df['foo'], df['bar'],
         right_index=True, left_index=True,
         suffixes=['_' + s for s in df.columns.levels[0].to_list()]
         ).to_sql('test', conn, if_exists='replace')

这会让你得到:

index                a_bar                  b_bar                  c_bar                  a_foo                  b_foo                  c_foo
-------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ----------------------
1                    0.989229457189419      0.0759829132299624     0.172846406489083      0.154227020200058      0.386003904079867      0.733402063652856
2                    0.839971061213949      0.975761261358953      0.252917398323633      0.0881692963378311     0.560403977291031      0.806066332511174
3                    0.914544313717528      0.921965094934119      0.821869705625485      0.337292501691803      0.125899685577926      0.527830968883373

【讨论】:

    【解决方案2】:

    我刚刚遇到了同样的问题。 Pandas 现在允许将索引或列多索引展平

    df.columns = df.columns.to_flat_index()

    先做,再做

    df.to_sql('test', conn, if_exists='replace')

    索引已写入,列名与您的 SQL 输出相同。

    如果您不喜欢奇怪的 SQL 列名,另一种选择是修改 pandas 列名,而不是加入两个级别,即。

    df.columns = ['_'.join(i) for i in df.columns.to_flat_index()]
    print(df.columns)
    Index(['foo_a', 'foo_b', 'foo_c', 'bar_a', 'bar_b', 'bar_c'], dtype='object')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 2018-12-21
      • 2014-01-09
      相关资源
      最近更新 更多