Create a pandas DataFrame from generator?

前端 未结 5 892
野的像风
野的像风 2020-12-08 07:07

I\'ve create a tuple generator that extract information from a file filtering only the records of interest and converting it to a tuple that generator returns.

I\'ve

5条回答
  •  没有蜡笔的小新
    2020-12-08 07:38

    You certainly can construct a pandas.DataFrame() from a generator of tuples, as of version 19 (and probably earlier). Don't use .from_records(); just use the constructor, for example:

    import pandas as pd
    someGenerator = ( (x, chr(x)) for x in range(48,127) )
    someDf = pd.DataFrame(someGenerator)
    

    Produces:

    type(someDf) #pandas.core.frame.DataFrame
    
    someDf.dtypes
    #0     int64
    #1    object
    #dtype: object
    
    someDf.tail(10)
    #      0  1
    #69  117  u
    #70  118  v
    #71  119  w
    #72  120  x
    #73  121  y
    #74  122  z
    #75  123  {
    #76  124  |
    #77  125  }
    #78  126  ~
    

提交回复
热议问题