将元组转换为数据帧

时间:2015-12-30 16:07:06

标签: python pandas

我有这样的元组:

x=(('a', 'b'), ('foo', 'bar'))

我想把它变成像这样的DataFrame:

One  Two   Three   Four
a    b     foo     bar

我一直试图用这个:

df = pd.DataFrame(x, columns=['One', 'Two', 'Three', 'Four])

但是会返回此错误:

runfile('D:/python codes/histo_matching.py', wdir='D:/python codes')
Traceback (most recent call last):

  File "<ipython-input-31-1104531b1d67>", line 1, in <module>
    runfile('D:/python codes/histo_matching.py', wdir='D:/python codes')

  File "C:\Users\Stefano\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "C:\Users\Stefano\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "D:/python codes/histo_matching.py", line 63, in <module>
    df = pd.DataFrame(x, columns=['One', 'Two', 'Three', 'Four'])

  File "C:\Users\Stefano\Anaconda\lib\site-packages\pandas\core\frame.py", line 291, in __init__
    raise PandasError('DataFrame constructor not properly called!')

PandasError: DataFrame constructor not properly called!

2 个答案:

答案 0 :(得分:3)

您可以使用list(sum(x, ()))展开tuple的{​​{1}}:

tuples

答案 1 :(得分:1)

$( document ).ready(function() {
    $("#addMoreFields").click(function(event) {
        event.preventDefault();

        $("#inputHolder").append('<input name="name" type="text" placeholder="Name"><br>');
        $("#inputHolder").append('<input name="phone" type="text" placeholder="Phone"><br>');
        $("#inputHolder").append('<input name="email" type="text" placeholder="E-Mail"><br><br>');
    });
});