假设我有一本看起来像这样的字典:
{Row1 : {Col1: Data, Col2: Data ..} , Row2: {Col1: Data,...},...}
Ex 字典:
{'1': {'0': '1', '1': '2', '2': '3'}, '2': {'0': '4', '1': '5', '2': '6'}}
我正在检查使用 orient='index' 的 pandas from_dict 方法,但这并不是我所需要的。
这就是我所拥有的:
df_pasted_data = pd.DataFrame()
for v in dictionary.values():
# need to set the index to 0 since im passing in a basic dictionary that looks like: col1: data, col2: data
# otherwise it will throw an error saying ValueError: If using all scalar values, you must pass an index
temp = pd.DataFrame(v, index=[0])
# append doesn't happen in place so i need to set it to itself
df_pasted_data = df_pasted_data.append(temp, ignore_index=True)
这行得通,但我在网上读到做追加和其他东西不是很有效,有没有更好的方法来解决这个问题?
答案 0 :(得分:1)
利用DataFrame()
方法和T
(Transpose)属性:
import pandas as pd
df_pasted_data=pd.DataFrame(dictionary).T
#output
print(df_pasted_data)
0 1 2
1 1 2 3
2 4 5 6