[英]How to convert a python dictionary with tuples into a pandas dataframe?
I have a python dictionary in which the keys of the dictionary are tuples of two strings and the values are integers.我有一个 python 字典,其中字典的键是两个字符串的元组,值是整数。
It looks like this:它看起来像这样:
mydic = { ('column1', 'index1'):33,
('column1', 'index2'):34,
('column2', 'index1'):35,
('column2', 'index2'):36 }
The first string of the tuples should be used as the column-name in the dataframe and the second string in the tuple should be used as the index.元组的第一个字符串应用作 dataframe 中的列名,元组中的第二个字符串应用作索引。
The dataframe from this should look like this: dataframe 应如下所示:
(index) (指数) | column1专栏 1 | column 2第 2 栏 |
---|---|---|
index1指数1 | 33 33 | 35 35 |
index2指数2 | 34 34 | 36 36 |
Is there any way to do this?有什么办法吗?
(Or do I have to loop through all elements of the dictionary and build the dataframe one value at a time by hand?) (或者我是否必须遍历字典的所有元素并一次手动构建 dataframe 一个值?)
Build apd.Series
first (which will have a MultiIndex), then use pd.Series.unstack
to get the column names.首先构建一个pd.Series
(它将有一个 MultiIndex),然后使用pd.Series.unstack
来获取列名。
df = pd.Series(mydic).unstack(0)
print(df)
column1 column2
index1 33 35
index2 34 36
You can use pd.MultiIndex.from_tuples
.您可以使用pd.MultiIndex.from_tuples
。
mydic = { ('column1', 'index1'):33,
('column1', 'index2'):34,
('column2', 'index1'):35,
('column2', 'index2'):36 }
df = pd.DataFrame(mydic.values(), index = pd.MultiIndex.from_tuples(mydic))
0
column1 index1 33
index2 34
column2 index1 35
index2 36
What comes after that is just a workaround.之后发生的只是一种解决方法。
df.T.stack()
column1 column2
0 index1 33 35
index2 34 36
Notice that the index contains two rows.请注意,索引包含两行。 Do not forget to reset it.不要忘记重置它。
df.T.stack().reset_index().drop('level_0', axis = 1)
level_1 column1 column2
0 index1 33 35
1 index2 34 36
You can rename the level_1
if you want to.如果需要,您可以重命名level_1
。 Hope it helps.希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.