add columns different length pandas

We can add the different size of list values to DataFrame.

Example

a = [0,1,2,3]
b = [0,1,2,3,4,5,6,7,8,9]
c = [0,1]

Find the Length of all list

la,lb,lc = len(a),len(b),len(c)
# now find the max
max_len = max(la,lb,lc)

Resize all according to the determined max length (not in this example

if not max_len == la:
  a.extend(['']*(max_len-la))
if not max_len == lb:
  b.extend(['']*(max_len-lb))
if not max_len == lc:
  c.extend(['']*(max_len-lc))

Now the all list is same length and create dataframe

pd.DataFrame({'A':a,'B':b,'C':c}) 

Final Output is

   A  B  C
0  1  0  1
1  2  1   
2  3  2   
3     3   
4     4   
5     5   
6     6   
7     7   
8     8   
9     9  

Use concat and pass axis=1 and ignore_index=True:

In [38]:

import numpy as np
df = pd.DataFrame({'a':np.arange(5)})
df1 = pd.DataFrame({'b':np.arange(4)})
print(df1)
df
   b
0  0
1  1
2  2
3  3
Out[38]:
   a
0  0
1  1
2  2
3  3
4  4
In [39]:

pd.concat([df,df1], ignore_index=True, axis=1)
Out[39]:
   0   1
0  0   0
1  1   1
2  2   2
3  3   3
4  4 NaN

If you use accepted answer, you'll lose your column names, as shown in the accepted answer example, and described in the documentation (emphasis added):

The resulting axis will be labeled 0, ..., n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information.

It looks like column names ('Name column') are meaningful to the Original Poster / Original Question.

To save column names, use pandas.concat, but don't ignore_index (default value of ignore_index is false; so you can omit that argument altogether). Continue to use axis=1:

import pandas

# Note these columns have 3 rows of values:
original = pandas.DataFrame({
    'Age':[10, 12, 13], 
    'Gender':['M','F','F']
})

# Note this column has 4 rows of values:
additional = pandas.DataFrame({
    'Name': ['Nate A', 'Jessie A', 'Daniel H', 'John D']
})

new = pandas.concat([original, additional], axis=1) 
# Identical:
# new = pandas.concat([original, additional], ignore_index=False, axis=1) 

print(new.head())

#          Age        Gender        Name
#0          10             M      Nate A
#1          12             F    Jessie A
#2          13             F    Daniel H
#3         NaN           NaN      John D

Notice how John D does not have an Age or a Gender.

Tags:

Python

Pandas