【问题标题】:Pandas multilevel index to and from sqlPandas 与 sql 之间的多级索引
【发布时间】:2017-06-09 19:00:37
【问题描述】:

我有一个多级列索引组对象,我正在尝试从 SQlite 数据库发送和检索。 Pandas 默认情况下将索引转换为看起来像元组的字符串(这很棒),但我遇到的问题是当表被读回时,多级索引丢失并且我留下了字符串元组作为列标题。

这是一个例子:

import pandas as pd
import numpy as np
import sqlite3

# Create a dataframe
data = {'Pets and Fruits'  : ["Apples", "Oranges", "Puppies", "Ducks"]*5,
        'C1'     : [1., 2., 3., 4.]*5,
        'C2'     : [1., 2., 3., 4.]*5,}
df = pd.DataFrame(data)

# Groupby dataframe
df = df.groupby("Pets and Fruits").agg(['sum', 'mean'])

# Create a sqlite database
db = sqlite3.connect("Fruits and Pets.sqlite")

# Send the group to the database
df.to_sql(name="fruits_and_pets", con=db, if_exists='replace')

# Read back the table
df_read = pd.read_sql_query('''SELECT * FROM fruits_and_pets''',con=db,index_col="Pets and Fruits")

print df
print df_read

在进入数据库之前:

                   C1         C2     
                  sum mean   sum mean
Pets and Fruits                      
Apples            5.0  1.0   5.0  1.0
Ducks            20.0  4.0  20.0  4.0
Oranges          10.0  2.0  10.0  2.0
Puppies          15.0  3.0  15.0  3.0

返回表单数据库:

                 ('C1', 'sum')  ('C1', 'mean')  ('C2', 'sum')  ('C2', 'mean')
Pets and Fruits                                                              
Apples                     5.0             1.0            5.0             1.0
Ducks                     20.0             4.0           20.0             4.0
Oranges                   10.0             2.0           10.0             2.0
Puppies                   15.0             3.0           15.0             3.0

我可以使用df_read.columns = pd.MultiIndex.from_tuples([eval(x) for x in df_read.columns]) 将数据框转回多级索引,但我想知道是否有更好的方法或我缺少的内置方法?

【问题讨论】:

  • 我建议使用ast.literal_eval() 而不是eval(),因为它可能用于注射...

标签: python sql sqlite pandas


【解决方案1】:
import pandas as pd
import numpy as np
import sqlite3

# Create a dataframe
data = {'Pets and Fruits'  : ["Apples", "Oranges", "Puppies", "Ducks"]*5,
        'C1'     : [1., 2., 3., 4.]*5,
        'C2'     : [1., 2., 3., 4.]*5,}
df = pd.DataFrame(data)

# Groupby dataframe
df = df.groupby("Pets and Fruits").agg(['sum', 'mean'])
df['Pets and Fruits'] = df.index.values
df = df.melt(id_vars='Pets and Fruits',var_name=['C','agg_type'])
# Create a sqlite database
db = sqlite3.connect("Fruits and Pets.sqlite")

# Send the group to the database
df.to_sql(name="fruits_and_pets", con=db, if_exists='replace',index=False)

# Read back the table
df_read = pd.read_sql_query('''SELECT * FROM fruits_and_pets''',con=db,index_col=["C","agg_type"])#,index_col=["Pets and Fruits",])
df_read = df_read.pivot(columns='Pets and Fruits').T
df_read.index = df_read.index.droplevel(0)
print(df)
print(df_read)

【讨论】:

    猜你喜欢
    • 2019-10-27
    • 2015-11-04
    • 2016-12-16
    • 1970-01-01
    • 2018-07-09
    • 2013-11-09
    • 2019-06-13
    • 2021-02-02
    • 2017-05-20
    相关资源
    最近更新 更多