如何迭代Pandas数据透视表? (多索引数据帧?)

时间:2015-01-22 19:43:26

标签: python pandas pivot-table

我有一个想要迭代的数据透视表,存储在数据库中。

                                           age  weekly_income
category_weekly_income category_age
High income            Middle aged   45.527721   15015.463667
                       Old           70.041456   14998.104486
                       Young         14.995210   15003.750822
Low income             Middle aged   45.548155    1497.228548
                       Old           70.049987    1505.655319
                       Young         15.013538    1501.718198
Middle income          Middle aged   45.516583    6514.830294
                       Old           69.977657    6494.626962
                       Young         15.020688    6487.661554

我玩过reshape,融化,各种for循环,黑暗中的语法刺,堆栈链,unstacks,reset_indexes等等。我最接近的是语法:

crosstab[1:2].age

有了这个,我可以拉出单个值单元格,但是我无法得到索引的值。

1 个答案:

答案 0 :(得分:1)

您不需要迭代数据框,Pandas已经提供了一种方法,可以通过DataFrame.to_sql(...)将数据框转换为sql。

或者,如果要手动将数据插入数据库,可以使用Pandas' to_csv(),例如:

我有这样的df:

df
                     A         B
first second                    
bar   one     0.826425 -1.126757
      two     0.682297  0.875014
baz   one    -1.714757 -0.436622
      two    -0.366858  0.341702
foo   one    -1.068390 -1.074582
      two     0.863934  0.043367
qux   one    -0.510881  0.215230
      two     0.760373  0.274389

# set header=False, and index=True to get the MultiIndex from pivot    
print df.to_csv(header=False, index=True)

bar,one,0.8264252111679552,-1.1267570930327846
bar,two,0.6822970851678805,0.8750144682657339
baz,one,-1.7147570530422946,-0.43662238320911956
baz,two,-0.3668584476904599,0.341701643567155
foo,one,-1.068390451744478,-1.0745823278191735
foo,two,0.8639343368644695,0.043366628502542914
qux,one,-0.5108806384876237,0.21522973766619563
qux,two,0.7603733646419842,0.2743886250125428

这将为您提供一个很好的逗号分隔格式,可以在sql execute query中轻松使用,如:

data = []
for line in df.to_csv(header=False, index=True).split('\n'):
    if line:
        data.append(tuple(line.split(',')))

data

[('bar', 'one', '0.8264252111679552', '-1.1267570930327846'),
 ('bar', 'two', '0.6822970851678805', '0.8750144682657339'),
 ('baz', 'one', '-1.7147570530422946', '-0.43662238320911956'),
 ('baz', 'two', '-0.3668584476904599', '0.341701643567155'),
 ('foo', 'one', '-1.068390451744478', '-1.0745823278191735'),
 ('foo', 'two', '0.8639343368644695', '0.043366628502542914'),
 ('qux', 'one', '-0.5108806384876237', '0.21522973766619563'),
 ('qux', 'two', '0.7603733646419842', '0.2743886250125428')]

然后它只是做executemany

的问题
...
stmt = "INSERT INTO table (first, second, A, B) VALUES (%s, %s, %s, %s)"
cursor.executemany(stmt, data)
...

希望这有帮助。

相关问题