3 回答
TA贡献1777条经验 获得超10个赞
我认为你需要的只是concat并unstack注意的d是你的dict
pd.concat(d).colB.unstack()
Out[663]:
colA str1 str2 str3 str4 str5 str6
k1 3.0 4.0 5.0 NaN NaN NaN
k2 NaN 3.0 4.0 NaN 5.0 NaN
k3 NaN NaN NaN 3.0 4.0 5.0
TA贡献1788条经验 获得超4个赞
查看示例代码:
import pandas as pd
key1 = {'str1':3,'str2':4,'str3':5}
key2 = {'str2':3,'str3':4,'str5':5}
key3 = {'str4':3,'str5':4,'str6':5}
df1=pd.DataFrame.from_dict(key1, orient='index')
df1.rename(columns={0:'key1'},inplace=True)
df2=pd.DataFrame.from_dict(key2, orient='index')
df2.rename(columns={0:'key2'},inplace=True)
df3=pd.DataFrame.from_dict(key3, orient='index')
df3.rename(columns={0:'key3'},inplace=True)
df = pd.concat([df1,df2,df3],axis=1)
df_final=df.T
df_final.head()
代码非常简单,如果您需要更多解释,请告诉我。
添加回答
举报