将Python字典重塑为Pandas数据框

时间:2019-01-25 15:30:12

标签: python pandas dictionary

我给了一个Python字典,看起来像:

myDict = {'A':['a','b','c','d','e'],
          'B':['f','g','h']}

我想将其转换为具有以下结构的Pandas数据框:

  key val
0   B   f
1   B   g
2   B   h
3   A   a
4   A   b
5   A   c
6   A   d
7   A   e

我无法使用标准方法将字典输入到Pandas数据框中,但是我想出了一种可行的方法,但看起来有些笨拙。基本上,我创建了2个包含键和值的列表,将其转换为另一个字典并将该字典导入到Pandas数据框中。我确定我可以使用列表或字典理解来合并某些行,但是最终命令将不可读。我暂时将每行分开,以使阅读代码更容易。

myDict = {'A':['a','b','c','d','e'],
          'B':['f','g','h']}

# Column of keys
keys = [[k]*len(v) for k,v in myDict.items()]

# Flatten list
keys = [item for sublist in keys for item in sublist]

# Column of values
values = [v for k,v in myDict.items()]

# Flatten list
values = [item for sublist in values for item in sublist]

key = 'key'
value = 'val'

df = pd.DataFrame({key:keys,value:values})

print(df)

也许我凝视了太久了,但是我的问题是是否有一个更简单的内置命令来实现相同的目标。

5 个答案:

答案 0 :(得分:4)

您可以简单地遍历字典,然后依次遍历每个列表:

class Menu():
    def __init__(self,master):
    ....
    def openwindow(self):
        secondwindow = my_sub_window(root)
    def dosomething(self):
        ....

class my_sub_window():
    def __init__(self, master):
        update_root()
    def update_root(self):
        dosomething()  # How can I call dosomething method in Menu() class?

root = Tk()
myApp = Menu(root)
root.mainloop()

答案 1 :(得分:1)

您可以使用单线执行此操作:

public class CustomInterceptor extends EmptyInterceptor {

    private static final long serialVersionUID = -8022068618978801796L;

    private String getTemporaryTableName() {
        String currentThreadName = Thread.currentThread().getName();
        return "##" + currentThreadName.replaceAll("[^A-Za-z0-9\\_]", "");
    }

    private void createTemporaryTable(Connection connection) {
        String tempTableName = this.getTemporaryTableName();
        String commandText = String.format("if (object_id('tempdb.dbo.%s') is null) begin create table [%s] ( dummyfield int ); insert into %s ( dummyfield ) values ( 0 ) end ", tempTableName, tempTableName, tempTableName);
        try (PreparedStatement statement = connection.prepareStatement(commandText)) {
            statement.execute();
            connection.commit();
        } catch (SQLException e) {
            throw new RuntimeException(String.format("An error has been occurred trying to create the temporary table %s", tempTableName), e);
        }
    }

    public CustomInterceptor(Connection connection) {
        this.createTemporaryTable(connection);
    }

    @Override
    public String onPrepareStatement(String sql) {
        int ps = sql.toLowerCase().indexOf("insert into ");
        if (ps == 0) {
            String tableName = this.getTemporaryTableName();
            return sql + "; if (@@rowcount = 0) update [" + tableName + "] set dummyfield = 1"; 
        }
        return super.onPrepareStatement(sql);
    }

}

答案 2 :(得分:0)

使用unnesting

df=unnesting(pd.Series(myDict).to_frame(),[0]).reset_index()
df.columns=['key','value']
df
Out[402]: 
  key value
0   A     a
1   A     b
2   A     c
3   A     d
4   A     e
5   B     f
6   B     g
7   B     h

答案 3 :(得分:0)

一个在大熊猫以外运行但略微简化操作的选项:

my_dict = {'A':['a','b','c','d','e'],
          'B':['f','g','h']}

my_vals=[list(zip(k*len(v), v)) for k,v in my_dict.items()]

my_rows=[item for sublist in my_vals for item in sublist ]

pd.DataFrame(my_rows, columns=['key','val'])

    key val
0   A   a
1   A   b
2   A   c
3   A   d
4   A   e
5   B   f
6   B   g
7   B   h

答案 4 :(得分:0)

您也可以尝试使用:

import pandas as pd

myDict = {'A':['a','b','c','d','e'],
          'B':['f','g','h']}

# Creating dataframe from the dictionary 
df = pd.DataFrame.from_dict(myDict, orient='index')

# Transpose to change keys to column names
# stack creates level by pivoting columns
# reset index and only select two columns
df = df.T.stack().reset_index().iloc[:,1:]

# renaming columns
df.columns = ['key', 'value']