Pandas:创建没有按字母顺序自动排序列名的数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39862053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas: create dataframe without auto ordering column names alphabetically
提问by hurrikale
I am creating an initial pandas dataframe to store results generated from other codes: e.g.
我正在创建一个初始的 Pandas 数据框来存储从其他代码生成的结果:例如
result = pd.DataFrame({'date': datelist, 'total': [0]*len(datelist),
'TT': [0]*len(datelist)})
with datelist
a predefined list. Then other codes will output some number for total
and TT
for each date
, which I will store in the result
dataframe.
带有datelist
预定义的列表。然后其他代码将输出一些数字 fortotal
和TT
for each date
,我将其存储在result
数据帧中。
So I want the first column to be date
, second total
and third TT
. However, pandas will automatically reorder it alphabetically to TT
, date
, total
at creation. While I can manually reorder this again afterwards, I wonder if there is an easier way to achieve this in one step.
所以我希望第一列是date
,第二total
和第三TT
。但是,pandas 会在创建时自动按字母顺序将其重新排序为TT
, date
, total
。虽然我可以在之后再次手动重新排序,但我想知道是否有一种更简单的方法可以一步实现这一目标。
I figured I can also do
我想我也可以
result = pd.DataFrame(np.transpose([datelist, [0]*l, [0]*l]),
columns = ['date', 'total', 'TT'])
but it somehow also looks tedious. Any other suggestions?
但不知何故,它看起来也很乏味。还有其他建议吗?
回答by wonce
You can pass the (correctly ordered) list of column as parameter to the constructor or use an OrderedDict:
您可以将(正确排序的)列列表作为参数传递给构造函数或使用 OrderedDict:
# option 1:
result = pd.DataFrame({'date': datelist, 'total': [0]*len(datelist),
'TT': [0]*len(datelist)}, columns=['date', 'total', 'TT'])
# option 2:
od = collections.OrderedDict()
od['date'] = datelist
od['total'] = [0]*len(datelist)
od['TT'] = [0]*len(datelist)
result = pd.DataFrame(od)
回答by u5475794
result = pd.DataFrame({'date': [23,24], 'total': 0,
'TT': 0},columns=['date','total','TT'])
回答by kadee
Use pandas >= 0.23 in combination with Python >= 3.6.
将 pandas >= 0.23 与 Python >= 3.6 结合使用。
result = pd.DataFrame({'date': datelist,
'total': [0]*len(datelist),
'TT': [0]*len(datelist)})
result = pd.DataFrame({'date': datelist,
'total': [0]*len(datelist),
'TT': [0]*len(datelist)})
retains the dict's insertion order when creating a DataFrame (or Series) from a dict when using pandas v0.23.0 in combination with Python3.6.
在结合使用 pandas v0.23.0 和 Python3.6 时,从 dict 创建 DataFrame(或系列)时保留 dict 的插入顺序。