简体   繁体   中英

How can I create Pandas DataFrame from a dict with lists with specific indexes and columns in Python3?

Assume now I have a dict with lists:

dic = { "protein1": ["func1", "func2"],
        "protein2": ["func2", "func3", "func5"],
        "protein3": ["func3", "func5"]}

and the list of index:

rows = ["protein1", "protein2", "protein3", "protein4"]

and the list of column:

columns = ["func1", "func2", "func3", "func4", "func5", "func6"]

I want to convert dic to a Pandas DataFrame like

           func1    func2     func3    func4   func5    func6
protein1     1        1          0       0       0        0
protein2     0        1          1       0       1        0
protein3     0        0          1       0       1        0
protein4     0        0          0       0       0        0

What's the pythonic way to code this? Thank you!

Use MultiLabelBinarizer with DataFrame.reindex :

from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()
df = (pd.DataFrame(mlb.fit_transform(dic.values()),columns=mlb.classes_, index=dic.keys())
        .reindex(columns=columns, index=rows, fill_value=0))
print (df)
          func1  func2  func3  func4  func5  func6
protein1      1      1      0      0      0      0
protein2      0      1      1      0      1      0
protein3      0      0      1      0      1      0
protein4      0      0      0      0      0      0

Only pandas solution is possible, but slowier - use Series.str.get_dummies :

df = (pd.Series({k:'|'.join(v) for k, v in dic.items()}).str.get_dummies()
        .reindex(columns=columns, index=rows, fill_value=0))

Another solution whose otput is a dataframe with boolean values (can be treated as integers)

import numpy as np 

dic = { "protein1": ["func1", "func2"], 
        "protein2": ["func2", "func3", "func5"], 
        "protein3": ["func3", "func5"]}  

columns = ["func1", "func2", "func3", "func4", "func5", "func6"]
n = len(columns)  

# index arrays by column values 
for key, value in dic.items(): 
      newRow = np.empty(n, dtype=bool) 
      np.put(newRow, [columns.index(i) for i in value], True) 
      dic[key] = newRow 

pd.DataFrame.from_dict(dic, orient='index', columns=columns)
# Out:
#           func1  func2  func3  func4  func5  func6
# protein1   True   True  False  False  False  False
# protein2  False   True   True  False   True  False
# protein3  False  False   True  False   True  False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM