0

I have an existing sample data frame (see below).

A   B
as  2
df  32
fj  1

I also have two sample lists:

list_1 = ['234', '341', '482']
list_2 = ['111', '2223', '8908']

I want to concat my two lists with my dataframe so that each element in the list is a column.

Desired output:

A   B   234   341   482   111   2223   8908
as  2
df  32
fj  1

I've scoured Google on how to do this but could not find anything specific.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
PineNuts0
  • 4,740
  • 21
  • 67
  • 112

1 Answers1

1

Use assign function:

df = pd.DataFrame([
    ['as', 2],
    ['df', 32],
    ['fj', 1]
])
list_1 = ['234', '341', '482']
list_2 = ['111', '2223', '8908']
df
    0   1
0   as  2
1   df  32
2   fj  1

df = df.assign(**{k: 0 for k in list_1 + list_2})
df

    0   1   111     2223    234     341     482     8908
0   as  2   0       0       0       0       0       0
1   df  32  0       0       0       0       0       0
2   fj  1   0       0       0       0       0       0
vurmux
  • 9,420
  • 3
  • 25
  • 45