如何创建具有特定列的空Pandas DataFrame?

时间:2015-06-04 17:12:28

标签: python pandas

我有一个DataFrame,它始终使用相同的列进行初始化,但有时并没有任何数据:

function addDevice(address, name, rssi)
{
    for (var i = 1; i<50; i++)
    {
        var $devices = $(".devices");

        var $check = $devices.find("li[data-address='{0}']".format(address));

        if ($check.length > 0)
            {
                return;
            }
        var template = $("#device").text().format(address, name, rssi);

        $devices.append(template);
        window.setTimeout(50000);

        if (rssi < -90){    
        alert(name + " loses proximity");
        }
    }       
}

但这失败了,错误:

import pandas as pd

data = []
df = pd.DataFrame(data, columns=['a', 'b', 'c'])

如何使用列创建空数据框?

1 个答案:

答案 0 :(得分:4)

As of Pandas version 0.16.1,传递data = []有效:

In [85]: df = pd.DataFrame([], columns=['a', 'b', 'c'])

In [86]: df
Out[86]: 
Empty DataFrame
Columns: [a, b, c]
Index: []

对于旧版本的Pandas,请勿传递任何data

In [69]: df = pd.DataFrame(columns=['a', 'b', 'c'])

In [70]: df
Out[70]: 
Empty DataFrame
Columns: [a, b, c]
Index: []

或通过data=None

In [71]: df = pd.DataFrame(None, columns=['a', 'b', 'c'])

In [72]: df
Out[72]: 
Empty DataFrame
Columns: [a, b, c]
Index: []