标题>
以下系列包含一个键值字典字符串。将列表拆分为a列和b列,如果列表为空,则a列和b列为0。否则,1.
<标题>输入:list = ["{'a': [], 'b': []}",
"{'a': [{'c':3,'d':56}], 'b': []}",
"{'a': [], 'b': [{'c':45,'d':67}]}",
"{'a': [{'c':48,'d':676}], 'b': [{'c':29,'d':90}]}" ]
df = pd.DataFrame(data=list,columns=['A'])
输出:
<表类>b tbody><<tr>0 0 10 0 1 11 表类>
另一个版本:
from ast import literal_eval
lst = [
"{'a': [], 'b': []}",
"{'a': [{'c':3,'d':56}], 'b': []}",
"{'a': [], 'b': [{'c':45,'d':67}]}",
"{'a': [{'c':48,'d':676}], 'b': [{'c':29,'d':90}]}",
]
df = pd.DataFrame(literal_eval(v) for v in lst)
df = df.applymap(bool).astype(int)
print(df)
打印:
a b
0 0 0
1 1 0
2 0 1
3 1 1
类似于@Andrej Kesely回答的解决方案-只删除了转换到列表和双DataFrame.astype
:
df = pd.DataFrame(df.A.tolist()).astype(bool).astype(int)
print (df)
a b
0 0 0
1 1 0
2 0 1
3 1 1