我试图将具有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')
这是一个错误还是我需要做其他事情来正确编写索引?
答案 0 :(得分: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表示例,则可以使用merge和后缀为每列:
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
答案 1 :(得分:0)
我遇到了同样的问题。熊猫现在允许使用索引或列多索引来展平
5
先这样做,然后
int
写入索引,并且列名与SQL输出相同。
如果您不喜欢奇怪的SQL列名称,另一种选择是修改熊猫列名称,而不是通过同时加入两个级别来实现。
df.columns = df.columns.to_flat_index()