How to convert a series of tuples into a pandas dataframe?












1















Assume that we have the following pandas series resulted from an apply function applied on a dataframe after groupby.



<class 'pandas.core.series.Series'>
0 (1, 0, [0.2, 0.2, 0.2], [0.2, 0.2, 0.2])
1 (2, 1000, [0.6, 0.7, 0.5], [0.1, 0.3, 0.1])
2 (1, 0, [0.4, 0.4, 0.4], [0.4, 0.4, 0.4])
3 (1, 0, [0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
4 (3, 14000, [0.8, 0.8, 0.8], [0.6, 0.6, 0.6])
dtype: object


Can we convert this into a dataframe when the sigList=['sig1','sig2', 'sig3'] are given?



Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
1 0 0.2 0.2 0.2 0.2 0.2 0.2
2 1000 0.6 0.7 0.5 0.1 0.3 0.1
1 0 0.4 0.4 0.4 0.4 0.4 0.4
1 0 0.5 0.5 0.5 0.5 0.5 0.5
3 14000 0.8 0.8 0.8 0.6 0.6 0.6


Thanks in advance










share|improve this question























  • def myfunc(x,signatures): return x.shape[0], x['start'].iloc[-1] - x['start'].iloc[0], x[signatures].agg(max), x[signatures].agg(min) This function creates the lists in tuples. Can we flatten the list in the return statement.

    – burcak
    Nov 20 '18 at 22:59


















1















Assume that we have the following pandas series resulted from an apply function applied on a dataframe after groupby.



<class 'pandas.core.series.Series'>
0 (1, 0, [0.2, 0.2, 0.2], [0.2, 0.2, 0.2])
1 (2, 1000, [0.6, 0.7, 0.5], [0.1, 0.3, 0.1])
2 (1, 0, [0.4, 0.4, 0.4], [0.4, 0.4, 0.4])
3 (1, 0, [0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
4 (3, 14000, [0.8, 0.8, 0.8], [0.6, 0.6, 0.6])
dtype: object


Can we convert this into a dataframe when the sigList=['sig1','sig2', 'sig3'] are given?



Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
1 0 0.2 0.2 0.2 0.2 0.2 0.2
2 1000 0.6 0.7 0.5 0.1 0.3 0.1
1 0 0.4 0.4 0.4 0.4 0.4 0.4
1 0 0.5 0.5 0.5 0.5 0.5 0.5
3 14000 0.8 0.8 0.8 0.6 0.6 0.6


Thanks in advance










share|improve this question























  • def myfunc(x,signatures): return x.shape[0], x['start'].iloc[-1] - x['start'].iloc[0], x[signatures].agg(max), x[signatures].agg(min) This function creates the lists in tuples. Can we flatten the list in the return statement.

    – burcak
    Nov 20 '18 at 22:59
















1












1








1








Assume that we have the following pandas series resulted from an apply function applied on a dataframe after groupby.



<class 'pandas.core.series.Series'>
0 (1, 0, [0.2, 0.2, 0.2], [0.2, 0.2, 0.2])
1 (2, 1000, [0.6, 0.7, 0.5], [0.1, 0.3, 0.1])
2 (1, 0, [0.4, 0.4, 0.4], [0.4, 0.4, 0.4])
3 (1, 0, [0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
4 (3, 14000, [0.8, 0.8, 0.8], [0.6, 0.6, 0.6])
dtype: object


Can we convert this into a dataframe when the sigList=['sig1','sig2', 'sig3'] are given?



Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
1 0 0.2 0.2 0.2 0.2 0.2 0.2
2 1000 0.6 0.7 0.5 0.1 0.3 0.1
1 0 0.4 0.4 0.4 0.4 0.4 0.4
1 0 0.5 0.5 0.5 0.5 0.5 0.5
3 14000 0.8 0.8 0.8 0.6 0.6 0.6


Thanks in advance










share|improve this question














Assume that we have the following pandas series resulted from an apply function applied on a dataframe after groupby.



<class 'pandas.core.series.Series'>
0 (1, 0, [0.2, 0.2, 0.2], [0.2, 0.2, 0.2])
1 (2, 1000, [0.6, 0.7, 0.5], [0.1, 0.3, 0.1])
2 (1, 0, [0.4, 0.4, 0.4], [0.4, 0.4, 0.4])
3 (1, 0, [0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
4 (3, 14000, [0.8, 0.8, 0.8], [0.6, 0.6, 0.6])
dtype: object


Can we convert this into a dataframe when the sigList=['sig1','sig2', 'sig3'] are given?



Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
1 0 0.2 0.2 0.2 0.2 0.2 0.2
2 1000 0.6 0.7 0.5 0.1 0.3 0.1
1 0 0.4 0.4 0.4 0.4 0.4 0.4
1 0 0.5 0.5 0.5 0.5 0.5 0.5
3 14000 0.8 0.8 0.8 0.6 0.6 0.6


Thanks in advance







python pandas dataframe series






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 22:32









burcakburcak

638




638













  • def myfunc(x,signatures): return x.shape[0], x['start'].iloc[-1] - x['start'].iloc[0], x[signatures].agg(max), x[signatures].agg(min) This function creates the lists in tuples. Can we flatten the list in the return statement.

    – burcak
    Nov 20 '18 at 22:59





















  • def myfunc(x,signatures): return x.shape[0], x['start'].iloc[-1] - x['start'].iloc[0], x[signatures].agg(max), x[signatures].agg(min) This function creates the lists in tuples. Can we flatten the list in the return statement.

    – burcak
    Nov 20 '18 at 22:59



















def myfunc(x,signatures): return x.shape[0], x['start'].iloc[-1] - x['start'].iloc[0], x[signatures].agg(max), x[signatures].agg(min) This function creates the lists in tuples. Can we flatten the list in the return statement.

– burcak
Nov 20 '18 at 22:59







def myfunc(x,signatures): return x.shape[0], x['start'].iloc[-1] - x['start'].iloc[0], x[signatures].agg(max), x[signatures].agg(min) This function creates the lists in tuples. Can we flatten the list in the return statement.

– burcak
Nov 20 '18 at 22:59














3 Answers
3






active

oldest

votes


















3














Do it the old fashioned (and fast) way, using a list comprehension:



columns = ("Length Distance sig1Max sig2Max" 
"sig3Max sig1Min sig2Min sig3Min").split()
df = pd.DataFrame([[a, b, *c, *d] for a,b,c,d in series.values], columns=columns)
print(df)
Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
0 1 0 0.2 0.2 0.2 0.2 0.2 0.2
1 2 1000 0.6 0.7 0.5 0.1 0.3 0.1
2 1 0 0.4 0.4 0.4 0.4 0.4 0.4
3 1 0 0.5 0.5 0.5 0.5 0.5 0.5
4 3 14000 0.8 0.8 0.8 0.6 0.6 0.6




Or, perhaps you meant, do it a little more dynamically



sigList = ['sig1', 'sig2', 'sig3']

columns = ['Length', 'Distance']
columns.extend(f'{s}{lbl}' for lbl in ('Max', 'Min') for s in sigList )

df = pd.DataFrame([[a,b,*c,*d] for a,b,c,d in series.values], columns=columns)
print(df)
Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
0 1 0 0.2 0.2 0.2 0.2 0.2 0.2
1 2 1000 0.6 0.7 0.5 0.1 0.3 0.1
2 1 0 0.4 0.4 0.4 0.4 0.4 0.4
3 1 0 0.5 0.5 0.5 0.5 0.5 0.5
4 3 14000 0.8 0.8 0.8 0.6 0.6 0.6





share|improve this answer


























  • Great, especially the dynamic one, since in my case sigList is resolved at runtime.

    – burcak
    Nov 21 '18 at 3:48











  • @burcak I had the order of the loops interchanged with the second one (sorry!) but you got the idea.

    – coldspeed
    Nov 21 '18 at 3:53











  • Thanks. In fact, the function below generates the series of tuples when called in apply method of grouped dataframe. Is there a way to just return dataframe so I won't need to create dataframe from series later on? def myfunc(x,signatures): return x.shape[0], x['start'].iloc[-1] - x['start'].iloc[0], x[signatures].agg(max), x[signatures].agg(min)

    – burcak
    Nov 21 '18 at 4:08





















1














You may check



newdf=pd.DataFrame(s.tolist())
newdf=pd.concat([newdf[[0,1]],pd.DataFrame(newdf[2].tolist()),pd.DataFrame(newdf[3].tolist())],1)
newdf.columns = [
"Length", "Distance", "sig1Max", "sig2Max", "sig3Max", "sig1Min", "sig2Min", "sig3Min"
]
newdf
Out[163]:
Length Distance sig1Max ... sig1Min sig2Min sig3Min
0 1 0 0.2 ... 0.2 0.2 0.2
1 2 1000 0.6 ... 0.1 0.3 0.1
2 1 0 0.4 ... 0.4 0.4 0.4
3 1 0 0.5 ... 0.5 0.5 0.5
4 3 14000 0.8 ... 0.6 0.6 0.6
[5 rows x 8 columns]





share|improve this answer
























  • Found a way as well!

    – coldspeed
    Nov 20 '18 at 23:26



















0














You can flatten each element and then convert each to a Series itself. Converting each element to a Series turns the main Series (s in the example below) into a DataFrame. Then just set the column names as you wish.



For example:



import pandas as pd

# load in your data
s = pd.Series([
(1, 0, [0.2, 0.2, 0.2], [0.2, 0.2, 0.2]),
(2, 1000, [0.6, 0.7, 0.5], [0.1, 0.3, 0.1]),
(1, 0, [0.4, 0.4, 0.4], [0.4, 0.4, 0.4]),
(1, 0, [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
(3, 14000, [0.8, 0.8, 0.8], [0.6, 0.6, 0.6]),
])

def flatten(x):
# note this is not very robust, but works for this case
return [x[0], x[1], *x[2], *x[3]]

df = s.apply(flatten).apply(pd.Series)
df.columns = [
"Length", "Distance", "sig1Max", "sig2Max", "sig3Max", "sig1Min", "sig2Min", "sig3Min"
]


Then you have df as:



   Length  Distance  sig1Max  sig2Max  sig3Max  sig1Min  sig2Min  sig3Min
0 1.0 0.0 0.2 0.2 0.2 0.2 0.2 0.2
1 2.0 1000.0 0.6 0.7 0.5 0.1 0.3 0.1
2 1.0 0.0 0.4 0.4 0.4 0.4 0.4 0.4
3 1.0 0.0 0.5 0.5 0.5 0.5 0.5 0.5
4 3.0 14000.0 0.8 0.8 0.8 0.6 0.6 0.6





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402584%2fhow-to-convert-a-series-of-tuples-into-a-pandas-dataframe%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Do it the old fashioned (and fast) way, using a list comprehension:



    columns = ("Length Distance sig1Max sig2Max" 
    "sig3Max sig1Min sig2Min sig3Min").split()
    df = pd.DataFrame([[a, b, *c, *d] for a,b,c,d in series.values], columns=columns)
    print(df)
    Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
    0 1 0 0.2 0.2 0.2 0.2 0.2 0.2
    1 2 1000 0.6 0.7 0.5 0.1 0.3 0.1
    2 1 0 0.4 0.4 0.4 0.4 0.4 0.4
    3 1 0 0.5 0.5 0.5 0.5 0.5 0.5
    4 3 14000 0.8 0.8 0.8 0.6 0.6 0.6




    Or, perhaps you meant, do it a little more dynamically



    sigList = ['sig1', 'sig2', 'sig3']

    columns = ['Length', 'Distance']
    columns.extend(f'{s}{lbl}' for lbl in ('Max', 'Min') for s in sigList )

    df = pd.DataFrame([[a,b,*c,*d] for a,b,c,d in series.values], columns=columns)
    print(df)
    Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
    0 1 0 0.2 0.2 0.2 0.2 0.2 0.2
    1 2 1000 0.6 0.7 0.5 0.1 0.3 0.1
    2 1 0 0.4 0.4 0.4 0.4 0.4 0.4
    3 1 0 0.5 0.5 0.5 0.5 0.5 0.5
    4 3 14000 0.8 0.8 0.8 0.6 0.6 0.6





    share|improve this answer


























    • Great, especially the dynamic one, since in my case sigList is resolved at runtime.

      – burcak
      Nov 21 '18 at 3:48











    • @burcak I had the order of the loops interchanged with the second one (sorry!) but you got the idea.

      – coldspeed
      Nov 21 '18 at 3:53











    • Thanks. In fact, the function below generates the series of tuples when called in apply method of grouped dataframe. Is there a way to just return dataframe so I won't need to create dataframe from series later on? def myfunc(x,signatures): return x.shape[0], x['start'].iloc[-1] - x['start'].iloc[0], x[signatures].agg(max), x[signatures].agg(min)

      – burcak
      Nov 21 '18 at 4:08


















    3














    Do it the old fashioned (and fast) way, using a list comprehension:



    columns = ("Length Distance sig1Max sig2Max" 
    "sig3Max sig1Min sig2Min sig3Min").split()
    df = pd.DataFrame([[a, b, *c, *d] for a,b,c,d in series.values], columns=columns)
    print(df)
    Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
    0 1 0 0.2 0.2 0.2 0.2 0.2 0.2
    1 2 1000 0.6 0.7 0.5 0.1 0.3 0.1
    2 1 0 0.4 0.4 0.4 0.4 0.4 0.4
    3 1 0 0.5 0.5 0.5 0.5 0.5 0.5
    4 3 14000 0.8 0.8 0.8 0.6 0.6 0.6




    Or, perhaps you meant, do it a little more dynamically



    sigList = ['sig1', 'sig2', 'sig3']

    columns = ['Length', 'Distance']
    columns.extend(f'{s}{lbl}' for lbl in ('Max', 'Min') for s in sigList )

    df = pd.DataFrame([[a,b,*c,*d] for a,b,c,d in series.values], columns=columns)
    print(df)
    Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
    0 1 0 0.2 0.2 0.2 0.2 0.2 0.2
    1 2 1000 0.6 0.7 0.5 0.1 0.3 0.1
    2 1 0 0.4 0.4 0.4 0.4 0.4 0.4
    3 1 0 0.5 0.5 0.5 0.5 0.5 0.5
    4 3 14000 0.8 0.8 0.8 0.6 0.6 0.6





    share|improve this answer


























    • Great, especially the dynamic one, since in my case sigList is resolved at runtime.

      – burcak
      Nov 21 '18 at 3:48











    • @burcak I had the order of the loops interchanged with the second one (sorry!) but you got the idea.

      – coldspeed
      Nov 21 '18 at 3:53











    • Thanks. In fact, the function below generates the series of tuples when called in apply method of grouped dataframe. Is there a way to just return dataframe so I won't need to create dataframe from series later on? def myfunc(x,signatures): return x.shape[0], x['start'].iloc[-1] - x['start'].iloc[0], x[signatures].agg(max), x[signatures].agg(min)

      – burcak
      Nov 21 '18 at 4:08
















    3












    3








    3







    Do it the old fashioned (and fast) way, using a list comprehension:



    columns = ("Length Distance sig1Max sig2Max" 
    "sig3Max sig1Min sig2Min sig3Min").split()
    df = pd.DataFrame([[a, b, *c, *d] for a,b,c,d in series.values], columns=columns)
    print(df)
    Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
    0 1 0 0.2 0.2 0.2 0.2 0.2 0.2
    1 2 1000 0.6 0.7 0.5 0.1 0.3 0.1
    2 1 0 0.4 0.4 0.4 0.4 0.4 0.4
    3 1 0 0.5 0.5 0.5 0.5 0.5 0.5
    4 3 14000 0.8 0.8 0.8 0.6 0.6 0.6




    Or, perhaps you meant, do it a little more dynamically



    sigList = ['sig1', 'sig2', 'sig3']

    columns = ['Length', 'Distance']
    columns.extend(f'{s}{lbl}' for lbl in ('Max', 'Min') for s in sigList )

    df = pd.DataFrame([[a,b,*c,*d] for a,b,c,d in series.values], columns=columns)
    print(df)
    Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
    0 1 0 0.2 0.2 0.2 0.2 0.2 0.2
    1 2 1000 0.6 0.7 0.5 0.1 0.3 0.1
    2 1 0 0.4 0.4 0.4 0.4 0.4 0.4
    3 1 0 0.5 0.5 0.5 0.5 0.5 0.5
    4 3 14000 0.8 0.8 0.8 0.6 0.6 0.6





    share|improve this answer















    Do it the old fashioned (and fast) way, using a list comprehension:



    columns = ("Length Distance sig1Max sig2Max" 
    "sig3Max sig1Min sig2Min sig3Min").split()
    df = pd.DataFrame([[a, b, *c, *d] for a,b,c,d in series.values], columns=columns)
    print(df)
    Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
    0 1 0 0.2 0.2 0.2 0.2 0.2 0.2
    1 2 1000 0.6 0.7 0.5 0.1 0.3 0.1
    2 1 0 0.4 0.4 0.4 0.4 0.4 0.4
    3 1 0 0.5 0.5 0.5 0.5 0.5 0.5
    4 3 14000 0.8 0.8 0.8 0.6 0.6 0.6




    Or, perhaps you meant, do it a little more dynamically



    sigList = ['sig1', 'sig2', 'sig3']

    columns = ['Length', 'Distance']
    columns.extend(f'{s}{lbl}' for lbl in ('Max', 'Min') for s in sigList )

    df = pd.DataFrame([[a,b,*c,*d] for a,b,c,d in series.values], columns=columns)
    print(df)
    Length Distance sig1Max sig2Max sig3Max sig1Min sig2Min sig3Min
    0 1 0 0.2 0.2 0.2 0.2 0.2 0.2
    1 2 1000 0.6 0.7 0.5 0.1 0.3 0.1
    2 1 0 0.4 0.4 0.4 0.4 0.4 0.4
    3 1 0 0.5 0.5 0.5 0.5 0.5 0.5
    4 3 14000 0.8 0.8 0.8 0.6 0.6 0.6






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 3:52

























    answered Nov 20 '18 at 23:26









    coldspeedcoldspeed

    135k23145231




    135k23145231













    • Great, especially the dynamic one, since in my case sigList is resolved at runtime.

      – burcak
      Nov 21 '18 at 3:48











    • @burcak I had the order of the loops interchanged with the second one (sorry!) but you got the idea.

      – coldspeed
      Nov 21 '18 at 3:53











    • Thanks. In fact, the function below generates the series of tuples when called in apply method of grouped dataframe. Is there a way to just return dataframe so I won't need to create dataframe from series later on? def myfunc(x,signatures): return x.shape[0], x['start'].iloc[-1] - x['start'].iloc[0], x[signatures].agg(max), x[signatures].agg(min)

      – burcak
      Nov 21 '18 at 4:08





















    • Great, especially the dynamic one, since in my case sigList is resolved at runtime.

      – burcak
      Nov 21 '18 at 3:48











    • @burcak I had the order of the loops interchanged with the second one (sorry!) but you got the idea.

      – coldspeed
      Nov 21 '18 at 3:53











    • Thanks. In fact, the function below generates the series of tuples when called in apply method of grouped dataframe. Is there a way to just return dataframe so I won't need to create dataframe from series later on? def myfunc(x,signatures): return x.shape[0], x['start'].iloc[-1] - x['start'].iloc[0], x[signatures].agg(max), x[signatures].agg(min)

      – burcak
      Nov 21 '18 at 4:08



















    Great, especially the dynamic one, since in my case sigList is resolved at runtime.

    – burcak
    Nov 21 '18 at 3:48





    Great, especially the dynamic one, since in my case sigList is resolved at runtime.

    – burcak
    Nov 21 '18 at 3:48













    @burcak I had the order of the loops interchanged with the second one (sorry!) but you got the idea.

    – coldspeed
    Nov 21 '18 at 3:53





    @burcak I had the order of the loops interchanged with the second one (sorry!) but you got the idea.

    – coldspeed
    Nov 21 '18 at 3:53













    Thanks. In fact, the function below generates the series of tuples when called in apply method of grouped dataframe. Is there a way to just return dataframe so I won't need to create dataframe from series later on? def myfunc(x,signatures): return x.shape[0], x['start'].iloc[-1] - x['start'].iloc[0], x[signatures].agg(max), x[signatures].agg(min)

    – burcak
    Nov 21 '18 at 4:08







    Thanks. In fact, the function below generates the series of tuples when called in apply method of grouped dataframe. Is there a way to just return dataframe so I won't need to create dataframe from series later on? def myfunc(x,signatures): return x.shape[0], x['start'].iloc[-1] - x['start'].iloc[0], x[signatures].agg(max), x[signatures].agg(min)

    – burcak
    Nov 21 '18 at 4:08















    1














    You may check



    newdf=pd.DataFrame(s.tolist())
    newdf=pd.concat([newdf[[0,1]],pd.DataFrame(newdf[2].tolist()),pd.DataFrame(newdf[3].tolist())],1)
    newdf.columns = [
    "Length", "Distance", "sig1Max", "sig2Max", "sig3Max", "sig1Min", "sig2Min", "sig3Min"
    ]
    newdf
    Out[163]:
    Length Distance sig1Max ... sig1Min sig2Min sig3Min
    0 1 0 0.2 ... 0.2 0.2 0.2
    1 2 1000 0.6 ... 0.1 0.3 0.1
    2 1 0 0.4 ... 0.4 0.4 0.4
    3 1 0 0.5 ... 0.5 0.5 0.5
    4 3 14000 0.8 ... 0.6 0.6 0.6
    [5 rows x 8 columns]





    share|improve this answer
























    • Found a way as well!

      – coldspeed
      Nov 20 '18 at 23:26
















    1














    You may check



    newdf=pd.DataFrame(s.tolist())
    newdf=pd.concat([newdf[[0,1]],pd.DataFrame(newdf[2].tolist()),pd.DataFrame(newdf[3].tolist())],1)
    newdf.columns = [
    "Length", "Distance", "sig1Max", "sig2Max", "sig3Max", "sig1Min", "sig2Min", "sig3Min"
    ]
    newdf
    Out[163]:
    Length Distance sig1Max ... sig1Min sig2Min sig3Min
    0 1 0 0.2 ... 0.2 0.2 0.2
    1 2 1000 0.6 ... 0.1 0.3 0.1
    2 1 0 0.4 ... 0.4 0.4 0.4
    3 1 0 0.5 ... 0.5 0.5 0.5
    4 3 14000 0.8 ... 0.6 0.6 0.6
    [5 rows x 8 columns]





    share|improve this answer
























    • Found a way as well!

      – coldspeed
      Nov 20 '18 at 23:26














    1












    1








    1







    You may check



    newdf=pd.DataFrame(s.tolist())
    newdf=pd.concat([newdf[[0,1]],pd.DataFrame(newdf[2].tolist()),pd.DataFrame(newdf[3].tolist())],1)
    newdf.columns = [
    "Length", "Distance", "sig1Max", "sig2Max", "sig3Max", "sig1Min", "sig2Min", "sig3Min"
    ]
    newdf
    Out[163]:
    Length Distance sig1Max ... sig1Min sig2Min sig3Min
    0 1 0 0.2 ... 0.2 0.2 0.2
    1 2 1000 0.6 ... 0.1 0.3 0.1
    2 1 0 0.4 ... 0.4 0.4 0.4
    3 1 0 0.5 ... 0.5 0.5 0.5
    4 3 14000 0.8 ... 0.6 0.6 0.6
    [5 rows x 8 columns]





    share|improve this answer













    You may check



    newdf=pd.DataFrame(s.tolist())
    newdf=pd.concat([newdf[[0,1]],pd.DataFrame(newdf[2].tolist()),pd.DataFrame(newdf[3].tolist())],1)
    newdf.columns = [
    "Length", "Distance", "sig1Max", "sig2Max", "sig3Max", "sig1Min", "sig2Min", "sig3Min"
    ]
    newdf
    Out[163]:
    Length Distance sig1Max ... sig1Min sig2Min sig3Min
    0 1 0 0.2 ... 0.2 0.2 0.2
    1 2 1000 0.6 ... 0.1 0.3 0.1
    2 1 0 0.4 ... 0.4 0.4 0.4
    3 1 0 0.5 ... 0.5 0.5 0.5
    4 3 14000 0.8 ... 0.6 0.6 0.6
    [5 rows x 8 columns]






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 23:17









    Wen-BenWen-Ben

    116k83369




    116k83369













    • Found a way as well!

      – coldspeed
      Nov 20 '18 at 23:26



















    • Found a way as well!

      – coldspeed
      Nov 20 '18 at 23:26

















    Found a way as well!

    – coldspeed
    Nov 20 '18 at 23:26





    Found a way as well!

    – coldspeed
    Nov 20 '18 at 23:26











    0














    You can flatten each element and then convert each to a Series itself. Converting each element to a Series turns the main Series (s in the example below) into a DataFrame. Then just set the column names as you wish.



    For example:



    import pandas as pd

    # load in your data
    s = pd.Series([
    (1, 0, [0.2, 0.2, 0.2], [0.2, 0.2, 0.2]),
    (2, 1000, [0.6, 0.7, 0.5], [0.1, 0.3, 0.1]),
    (1, 0, [0.4, 0.4, 0.4], [0.4, 0.4, 0.4]),
    (1, 0, [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
    (3, 14000, [0.8, 0.8, 0.8], [0.6, 0.6, 0.6]),
    ])

    def flatten(x):
    # note this is not very robust, but works for this case
    return [x[0], x[1], *x[2], *x[3]]

    df = s.apply(flatten).apply(pd.Series)
    df.columns = [
    "Length", "Distance", "sig1Max", "sig2Max", "sig3Max", "sig1Min", "sig2Min", "sig3Min"
    ]


    Then you have df as:



       Length  Distance  sig1Max  sig2Max  sig3Max  sig1Min  sig2Min  sig3Min
    0 1.0 0.0 0.2 0.2 0.2 0.2 0.2 0.2
    1 2.0 1000.0 0.6 0.7 0.5 0.1 0.3 0.1
    2 1.0 0.0 0.4 0.4 0.4 0.4 0.4 0.4
    3 1.0 0.0 0.5 0.5 0.5 0.5 0.5 0.5
    4 3.0 14000.0 0.8 0.8 0.8 0.6 0.6 0.6





    share|improve this answer




























      0














      You can flatten each element and then convert each to a Series itself. Converting each element to a Series turns the main Series (s in the example below) into a DataFrame. Then just set the column names as you wish.



      For example:



      import pandas as pd

      # load in your data
      s = pd.Series([
      (1, 0, [0.2, 0.2, 0.2], [0.2, 0.2, 0.2]),
      (2, 1000, [0.6, 0.7, 0.5], [0.1, 0.3, 0.1]),
      (1, 0, [0.4, 0.4, 0.4], [0.4, 0.4, 0.4]),
      (1, 0, [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
      (3, 14000, [0.8, 0.8, 0.8], [0.6, 0.6, 0.6]),
      ])

      def flatten(x):
      # note this is not very robust, but works for this case
      return [x[0], x[1], *x[2], *x[3]]

      df = s.apply(flatten).apply(pd.Series)
      df.columns = [
      "Length", "Distance", "sig1Max", "sig2Max", "sig3Max", "sig1Min", "sig2Min", "sig3Min"
      ]


      Then you have df as:



         Length  Distance  sig1Max  sig2Max  sig3Max  sig1Min  sig2Min  sig3Min
      0 1.0 0.0 0.2 0.2 0.2 0.2 0.2 0.2
      1 2.0 1000.0 0.6 0.7 0.5 0.1 0.3 0.1
      2 1.0 0.0 0.4 0.4 0.4 0.4 0.4 0.4
      3 1.0 0.0 0.5 0.5 0.5 0.5 0.5 0.5
      4 3.0 14000.0 0.8 0.8 0.8 0.6 0.6 0.6





      share|improve this answer


























        0












        0








        0







        You can flatten each element and then convert each to a Series itself. Converting each element to a Series turns the main Series (s in the example below) into a DataFrame. Then just set the column names as you wish.



        For example:



        import pandas as pd

        # load in your data
        s = pd.Series([
        (1, 0, [0.2, 0.2, 0.2], [0.2, 0.2, 0.2]),
        (2, 1000, [0.6, 0.7, 0.5], [0.1, 0.3, 0.1]),
        (1, 0, [0.4, 0.4, 0.4], [0.4, 0.4, 0.4]),
        (1, 0, [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
        (3, 14000, [0.8, 0.8, 0.8], [0.6, 0.6, 0.6]),
        ])

        def flatten(x):
        # note this is not very robust, but works for this case
        return [x[0], x[1], *x[2], *x[3]]

        df = s.apply(flatten).apply(pd.Series)
        df.columns = [
        "Length", "Distance", "sig1Max", "sig2Max", "sig3Max", "sig1Min", "sig2Min", "sig3Min"
        ]


        Then you have df as:



           Length  Distance  sig1Max  sig2Max  sig3Max  sig1Min  sig2Min  sig3Min
        0 1.0 0.0 0.2 0.2 0.2 0.2 0.2 0.2
        1 2.0 1000.0 0.6 0.7 0.5 0.1 0.3 0.1
        2 1.0 0.0 0.4 0.4 0.4 0.4 0.4 0.4
        3 1.0 0.0 0.5 0.5 0.5 0.5 0.5 0.5
        4 3.0 14000.0 0.8 0.8 0.8 0.6 0.6 0.6





        share|improve this answer













        You can flatten each element and then convert each to a Series itself. Converting each element to a Series turns the main Series (s in the example below) into a DataFrame. Then just set the column names as you wish.



        For example:



        import pandas as pd

        # load in your data
        s = pd.Series([
        (1, 0, [0.2, 0.2, 0.2], [0.2, 0.2, 0.2]),
        (2, 1000, [0.6, 0.7, 0.5], [0.1, 0.3, 0.1]),
        (1, 0, [0.4, 0.4, 0.4], [0.4, 0.4, 0.4]),
        (1, 0, [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
        (3, 14000, [0.8, 0.8, 0.8], [0.6, 0.6, 0.6]),
        ])

        def flatten(x):
        # note this is not very robust, but works for this case
        return [x[0], x[1], *x[2], *x[3]]

        df = s.apply(flatten).apply(pd.Series)
        df.columns = [
        "Length", "Distance", "sig1Max", "sig2Max", "sig3Max", "sig1Min", "sig2Min", "sig3Min"
        ]


        Then you have df as:



           Length  Distance  sig1Max  sig2Max  sig3Max  sig1Min  sig2Min  sig3Min
        0 1.0 0.0 0.2 0.2 0.2 0.2 0.2 0.2
        1 2.0 1000.0 0.6 0.7 0.5 0.1 0.3 0.1
        2 1.0 0.0 0.4 0.4 0.4 0.4 0.4 0.4
        3 1.0 0.0 0.5 0.5 0.5 0.5 0.5 0.5
        4 3.0 14000.0 0.8 0.8 0.8 0.6 0.6 0.6






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 22:41









        Henry WoodyHenry Woody

        4,7273927




        4,7273927






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402584%2fhow-to-convert-a-series-of-tuples-into-a-pandas-dataframe%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)