python - Append a list of arrays as column to pandas Data Frame with same column indices -
i have list of arrays (one-dimensional numpy array) (a_) , list (l_) , want have dataframe them columns. this:
a_: [array([381]), array([376]), array([402]), array([400])...] l_: [1.5,2.34,4.22,...]
i can by:
df_l = pd.dataframe(l_) df_a = pd.dataframe(a_) df = pd.concat([df_l, df_a], axis=1)
is there shorter way of doing it? tried use pd.append
:
df_l = pd.dataframe(l_) df_l = df_l.append(a_)
however, because columns indices both 0, adds a_ end of dataframe column, resulting in single column. there this:
l_ = l_.append(a_).reset(columns)
that set new column index appended array? well, not work!
the desired output like:
0 0 0 1.50 381 1 2.34 376 2 4.22 402
...
thanks.
suggestion:
df_l = pd.dataframe(l_) df_1['a_'] = pd.series(a_list, index=df_1.index)
example #1:
l = list(data) = list(data) data_frame = pd.dataframe(l) data_frame['a'] = pd.series(a, index=data_frame.index)
example #2 - same series length (create series , set index same existing data frame):
in [33]: l = list(item item in range(10)) in [34]: = list(item item in range(10,20)) in [35]: data_frame = pd.dataframe(l,columns=['l']) in [36]: data_frame['a'] = pd.series(a, index=data_frame.index) in [37]: print data_frame l 0 0 10 1 1 11 2 2 12 3 3 13 4 4 14 5 5 15 6 6 16 7 7 17 8 8 18 9 9 19
example #3 - different series lengths (create series , let pandas handle index matching):
in [45]: not_same_length = list(item item in range(50,55)) in [46]: data_frame['nsl'] = pd.series(not_same_length) in [47]: print data_frame l nsl 0 0 10 50 1 1 11 51 2 2 12 52 3 3 13 53 4 4 14 54 5 5 15 nan 6 6 16 nan 7 7 17 nan 8 8 18 nan 9 9 19 nan
based on comments, looks want join list of lists.i'm assuming in list structure because array()
not method in python. following:
in [63]: = [[381],[376], [402], [400]] in [64]: = [inner_item item in inner_item in item] in [65]: print [381, 376, 402, 400]
then create series using new array , follow steps above add data frame.
Comments
Post a Comment