Saturday, 15 May 2010

python - Append a list of arrays as column to pandas Data Frame with same column indices -


I have a list of arrays (one-dimensional numerical array) (a_) and a list (L_) and want They have a data frame with them in the form of their columns:

  a_: [array ([381]), array ([376]), array ([402]), array ([400]) ...] l_: [1.5,2.34,4.22, ...]  

I can do this:

 < Code> df_l = pd.DataFrame (l_) Df_a = pd.DataFrame (a_) df = pd.concat ([df_l, df_a], axis = 1)  

Is there a small way to do this? I tried to use pd.append :

  df_l = pd.DataFrame (l_) df_l = df_l.append (a_)  

However, because the column index is both 0, this data adds a_ at the end of the column column, which results in a column. Is there something like this:

l_ = l_.append (a _). Reset (column)

Is this a set index new column for the attached array? OK, obviously it does not work!

The desired output is the same:

0 0 1.50 381 1 2.34 376 2 4.22 402

...

Thank you.

Tip:

  df_l = pd.DataFrame (l_) df_1 ['A_'] = pd.Series (a_list, index = df_1.index)  

example # 1:

  L = list (data) A = List (data) data_frame = pd.DataFrame (L) data_frame ['a'] = pd.Series (A, index = example # 2 - equal series length (create series as current data frame and set index): In the   [33]: L = list (item for category in item (10)) [34]: A = list (items for range in item (10,20)) [35] in: data_frame = pd.DataFrame (L, columns = ['L']) [36]: data_frame ['a'] = pd.Series (A, index = data_frame.index) [37]: print data_frame LA 0 1 10 1 1 11 2 2 12 3 3 13 4 4 14 5 5 15 6 6 16 7 7 17 8 8 Examples 9 3 9 - Various series length (create series and handle pandas with index matches):  
 in  [45]: not_same_length = list (item in category (Items for 50,55) [46]: data_frame ['nsl'] = pd.Series (not_same_length) [47]: print data_frame LA nsl 0 10 50 1 1 11 51 2 2 12 52 3 3 13 53 4 4 14 54 5 5 15 Nain 6 6 16 Nain 7 7 17 Nain 8 8 18 Nain 9 919 NAN Based on your comment, it looks like you join your list of lists Received want. I think that they are in the list structure, because there is not a method in  array ()  Python. To do this, you will do the following: 

 In  [63]: In A = [[381], [376], [402], [400]] [64]: A = [In item for item in Inner IIT in IIT] [65]: Print A [381, 376, 402, 400]  

Then create a series using the new array and follow the steps to add up to your data frame.


No comments:

Post a Comment