将字典转换为pandas数据帧

时间:2017-02-26 09:06:59

标签: python pandas dictionary dataframe

我正在尝试将只有1条记录的字典转换为pandas数据帧。我使用了其他解决方案中的以下代码:

d =  {'id': 'CS2_056', 'cost': 2, 'name': 'Tap'}

pd.DataFrame(d.items(), columns=['id', 'cost','name'])

但是我收到以下错误:

PandasError: DataFrame constructor not properly called!

6 个答案:

答案 0 :(得分:27)

你的dict只有一个记录使用列表:

int[] elements;
int top;
Linkedlists min;

public Stack(int n) {
    elements = new int[n];
    top = 0;
    min = new Linkedlists();
}

public void realloc(int n) {
    int[] tab = new int[n];
    for (int i = 0; i < top; i++) {
        tab[i] = elements[i];
    }

    elements = tab;
}

public void push(int x) {
    if (top == elements.length) {
        realloc(elements.length * 2);
    }
    if (top == 0) {
        min.pre(x);
    } else if (x < min.head.data) {
        min.pre(x);
    } else {
        min.app(x);
    }
    elements[top++] = x;
}

public int pop() {

    int x = elements[--top];
    if (top == 0) {

    }
    if (this.getMin() == x) {
        min.head = min.head.next;
    }
    elements[top] = 0;
    if (4 * top < elements.length) {
        realloc((elements.length + 1) / 2);
    }

    return x;
}

public void display() {
    for (Object x : elements) {
        System.out.print(x + " ");
    }

}

public int getMin() {
    if (top == 0) {
        return 0;
    }
    return this.min.head.data;
}

public static void main(String[] args) {
    Stack stack = new Stack(4);
    stack.push(2);
    stack.push(3);
    stack.push(1);
    stack.push(4);
    stack.push(5);
    stack.pop();
    stack.pop();
    stack.pop();
    stack.push(1);
    stack.pop();
    stack.pop();
    stack.pop();
    stack.push(2);
    System.out.println(stack.getMin());
    stack.display();

}

输出:

import pandas as pd
d =  {'id': 'CS2_056', 'cost': 2, 'name': 'Tap'}
df = pd.DataFrame([d], columns=d.keys())
print df

答案 1 :(得分:5)

你可能正在使用python3。 在python3中我们有列表

extension UIViewController: CuteProtocol {
    // your code conforming to Cute Protocol goes here.
}

答案 2 :(得分:2)

使用字典中的单行创建数据框的另一种方法是先创建一个空数据框,然后再创建appending

import pandas as pd 
d =  {'id': 'CS2_056', 'cost': 2, 'name': 'Tap'}
df = pd.DataFrame().append(d, ignore_index=True)
print(df)

   cost       id name
0   2.0  CS2_056  Tap

请注意,此方法明显慢于@Serenity的解决方案,因此如果您担心性能,请务必不要选择此方法。 但有选择总是很好。

答案 3 :(得分:2)

简单的命令,只需确保将字典括在方括号中即可。

d =  {'id': 'CS2_056', 'cost': 2, 'name': 'Tap'}

df = pd.DataFrame([d])
df

答案 4 :(得分:1)

虽然这个问题确实有重复(Python Dictionary to Pandas Dataframe),但我相信答案比那里提供的答案更为简单。

将值转换为列表:

def build_leg(): import Limb.Leg reload(Limb.Leg) leg = Limb.Leg.Leg() def build_arm(): import Limb.Arm reload(Limb.Arm) arm = Limb.Arm.Arm() build_leg() build_arm()

然后简单地说:

if(isset($_POST['submit']))
{
$link=mysql_connect("localhost","root","");
mysql_select_db("db2017",$link);


$source = fopen('excdemo.xlsx', 'r') or die("Problem open file");
    while (($data = fgetcsv($source, 1000, ",")) !== FALSE)
    {
        $name = $data[0];
        $lname = $data[1];
        $phone = $data[2];
        $email = $data[3];
        $address = $data[4];
        $high = $data[5];


 mysql_query("INSERT INTO excel(fname,lname,phone,email,address,high) VALUES ('".$name."','".$lname."','".$phone."','".$email."','".$address."','".$high."') ");
    }
    fclose($source);
}


请注意,如果列顺序无关紧要,您仍需要明确向d = {'id': ['CS2_056'], 'cost': [2], 'name': ['Tap']}提供df = pd.DataFrame(d) print(df) # cost id name # 0 2 CS2_056 Tap

columns

答案 5 :(得分:-1)

将索引参数传递给 DataFrame 构造函数适用于 python 3。

import pandas as pd

d =  {'id': 'CS2_056', 'cost': 2, 'name': 'Tap'}
df = pd.DataFrame(d, index=[0])
print(df)

# Output:
#           id  cost name
#   0  CS2_056     2  Tap
相关问题