2

I want to add a list as a column to the df dataframe. The list has a different size than the column length.

df = 
A   B   C
1   2   3
5   6   9
4   
6       6
8       4
2       3
4   
6       6
8       4


D = [11,17,18]

I want the following output

df = 
A   B   C   D
1   2   3   11
5   6   9   17
4           18
6       6
8       4
2       3
4   
6       6
8       4

I am doing the following to extend the list to the size of the dataframe by adding "nan"

# number of nan value require for the list to match the size of the column
extend_length = df.shape[0]-len(D)

# extend the list 
D.extend(extend_length * ['nan'])

# add to the dataframe
df["D"] = D

A   B   C   D
1   2   3   11
5   6   9   17
4           18
6       6   nan
8       4   nan
2       3   nan
4           nan
6       6   nan
8       4   nan

Where "nan" is treated like string but I want it to be empty ot "nan", thus, if I search for number of valid cell in D column it will provide output of 3.

Sourav
  • 816
  • 3
  • 11
  • 26
  • Possible duplicate of [Adding list with different length as a new column to a dataframe](https://stackoverflow.com/questions/51424453/adding-list-with-different-length-as-a-new-column-to-a-dataframe) – TomNash Jul 10 '19 at 19:44

2 Answers2

2

Adding the list as a Series will handle this directly.

D = [11,17,18]
df.loc[:, 'D'] = pd.Series(D)
TomNash
  • 3,147
  • 2
  • 21
  • 57
1

A simple pd.concat on df and series of D as follows:

pd.concat([df, pd.Series(D, name='D')], axis=1)

or

df.assign(D=pd.Series(D))

Out[654]:
   A    B    C     D
0  1  2.0  3.0  11.0
1  5  6.0  9.0  17.0
2  4  NaN  NaN  18.0
3  6  NaN  6.0   NaN
4  8  NaN  4.0   NaN
5  2  NaN  3.0   NaN
6  4  NaN  NaN   NaN
7  6  NaN  6.0   NaN
8  8  NaN  4.0   NaN
Andy L.
  • 24,909
  • 4
  • 17
  • 29