将数据框中的字典列表转换为 CSV
Convert list of dict in dataframe to CSV
我有一个看起来像这样的数据框 (df1):
id detail
78 [{}{}{}{}{}]
120 [{}{}{}{}{}]
110 [{}{}{}{}{}]
109 [{}{}{}{}{}]
109 [{}{}{}{}{}]
79 [{}{}{}{}{}]
详细信息列包含字典列表,每个字典如下所示:
{'y1': 549, 'score': 1, 'x2': 630, 'frame': 1054, 'y2': 564, 'x1': 602, 'visibility': 0.0, 'class': 5}
我需要将此信息提取到格式如下的 CSV 文件中:
frame, id, x1, y1, x2, y2, score, class, visibility
另外,提取出来的数据中的x2和y2应该是这样的:
x2_new = x2 + x1 = 630 + 602 = 1232
y2_new = y2 + y1 = 564 + 549 = 1113
预期输出(假设提供的字典在 df1 的第一行):
1054, 78, 602, 549, 1232, 1113, 1, 5, 0.0
我已尝试使用此代码基于详细信息列创建新的 df,但出现错误:
for i in finaldftoconvert['id']:
for k in finaldftoconvert[['detail'][['id']==i]]:
df = pd.DataFrame(k)
print df
错误:
main.py:267: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
for k in finaldftoconvert[['detail'][['id']==i]]:
Traceback (most recent call last):
File "main.py", line 268, in <module>
df = pd.DataFrame(k)
File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 305, in __init__
raise PandasError('DataFrame constructor not properly called!')
pandas.core.common.PandasError: DataFrame constructor not properly called!
a = pd.DataFrame(index=[78],columns=["detail"])
a.loc[78,"detail"] = [{'y1': 549, 'score': 1, 'x2': 630, 'frame': 1054, 'y2': 564, 'x1': 602, 'visibility': 0.0, 'class': 5}]
a.loc[188,"detail"] = [{'y1': 649, 'score': 1, 'x2': 630, 'frame': 1054, 'y2': 564, 'x1': 602, 'visibility': 0.0, 'class': 5}]
对于每个字典,您应该使用 pd.DataFrame.from_dict
。
事实上,我不完全是你想打印出来?或将其转换为多个数据帧。
这里有一些简单的解决方案。
# print it
a.applymap(lambda x:print(pd.DataFrame.from_dict({0:x[0]})))
# convert it
for i in a.index:
tmp = pd.DataFrame.from_dict({0:a.loc[i,"detail"][0]}).T
tmp.x2 = tmp.x2+tmp.x1
tmp.y2 = tmp.y2 + tmp.y1
# this function you could storge in any dict/list. Or you could storge into a list. And using pd.concat to concate them together.
我有一个看起来像这样的数据框 (df1):
id detail
78 [{}{}{}{}{}]
120 [{}{}{}{}{}]
110 [{}{}{}{}{}]
109 [{}{}{}{}{}]
109 [{}{}{}{}{}]
79 [{}{}{}{}{}]
详细信息列包含字典列表,每个字典如下所示:
{'y1': 549, 'score': 1, 'x2': 630, 'frame': 1054, 'y2': 564, 'x1': 602, 'visibility': 0.0, 'class': 5}
我需要将此信息提取到格式如下的 CSV 文件中:
frame, id, x1, y1, x2, y2, score, class, visibility
另外,提取出来的数据中的x2和y2应该是这样的:
x2_new = x2 + x1 = 630 + 602 = 1232
y2_new = y2 + y1 = 564 + 549 = 1113
预期输出(假设提供的字典在 df1 的第一行):
1054, 78, 602, 549, 1232, 1113, 1, 5, 0.0
我已尝试使用此代码基于详细信息列创建新的 df,但出现错误:
for i in finaldftoconvert['id']:
for k in finaldftoconvert[['detail'][['id']==i]]:
df = pd.DataFrame(k)
print df
错误:
main.py:267: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
for k in finaldftoconvert[['detail'][['id']==i]]:
Traceback (most recent call last):
File "main.py", line 268, in <module>
df = pd.DataFrame(k)
File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 305, in __init__
raise PandasError('DataFrame constructor not properly called!')
pandas.core.common.PandasError: DataFrame constructor not properly called!
a = pd.DataFrame(index=[78],columns=["detail"])
a.loc[78,"detail"] = [{'y1': 549, 'score': 1, 'x2': 630, 'frame': 1054, 'y2': 564, 'x1': 602, 'visibility': 0.0, 'class': 5}]
a.loc[188,"detail"] = [{'y1': 649, 'score': 1, 'x2': 630, 'frame': 1054, 'y2': 564, 'x1': 602, 'visibility': 0.0, 'class': 5}]
对于每个字典,您应该使用 pd.DataFrame.from_dict
。
事实上,我不完全是你想打印出来?或将其转换为多个数据帧。
这里有一些简单的解决方案。
# print it
a.applymap(lambda x:print(pd.DataFrame.from_dict({0:x[0]})))
# convert it
for i in a.index:
tmp = pd.DataFrame.from_dict({0:a.loc[i,"detail"][0]}).T
tmp.x2 = tmp.x2+tmp.x1
tmp.y2 = tmp.y2 + tmp.y1
# this function you could storge in any dict/list. Or you could storge into a list. And using pd.concat to concate them together.