我有两个数据框,例如:
df1:
id A B C D
1 a b c d
1 e f g h
1 i j k l
df2:
id A C D
2 x y z
2 u v w
最终结果应该是:
id A B C D
1 a b c d
1 e f g h
1 i j k l
2 x y z
2 u v w
这些表是使用 json 文件中的 for 循环生成的。所以必须继续将这些表附加到另一个下面。
Note
:两个数据框的“id”列总是不同的。
我的做法:
data 是一个数据框,其中“X”列具有 json 数据,并且还具有“id”列。
df1=pd.DataFrame()
for i, row1 in data.head(2).iterrows():
df2= pd.io.json.json_normalize(row1["X"])
df2.columns = df2.columns.map(lambda x: x.split(".")[-1])
df2["id"]=[row1["id"] for i in range(df2.shape[0])]
if len(df1)==0:
df1=df2.copy()
df1=pd.concat((df1,df2), ignore_index=True)
Error: AssertionError: Number of manager items must equal union of block items # manager items: 46, # tot_items: 49
如何使用 python 或 pandas sql 解决这个问题。