根据字典元素创建Pandas数据框 [英] Creating a Pandas dataframe from elements of a dictionary
本文介绍了根据字典元素创建Pandas数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据字典创建熊猫数据框.字典设置为
I'm trying to create a pandas dataframe from a dictionary. The dictionary is set up as
nvalues = {"y1": [1, 2, 3, 4], "y2": [5, 6, 7, 8], "y3": [a, b, c, d]}
我希望数据框仅包含"y1"
和" y2"
.到目前为止,我可以使用
I would like the dataframe to include only "y1"
and "y2"
. So far I can accomplish this using
df = pd.DataFrame.from_dict(nvalues)
df.drop("y3", axis=1, inplace=True)
我想知道是否可以在没有 df.drop()
I would like to know if it is possible to accomplish this without having df.drop()
推荐答案
您可以在 DataFrame
构造函数中指定列
:
You can specify columns
in the DataFrame
constructor:
pd.DataFrame(nvalues, columns=('y1', 'y2'))
y1 y2
0 1 5
1 2 6
2 3 7
3 4 8
这篇关于根据字典元素创建Pandas数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文