TiiTcHY
TiiTcHY

Reputation: 69

Pandas add column from list with bigger index

I have a dataframe that only has 1 row:

TotalCount                 Percentage
57                              34

List1:

['Group1','Group2','Group3','Group4']

I have a list which contains 4 values. I want to add another column to my dataframe which contains the list:.

Desired Output:

TotalCount                 Percentage               Group
57                              34                  Group1
                                                    Group2
                                                    Group3
                                                    Group4

I have tried pd.Series(List1) but it only gives me 1 result

and if I try df['Group'] = List1 I get Length of values (4) does not match of length of index (1)

I want to be able to add the list to the DataFrame in a new column and ignore the index

Upvotes: 0

Views: 988

Answers (1)

RMRiver
RMRiver

Reputation: 645

Use pd.concat

df = pd.DataFrame({'a': {0: 57}, 'b': {0: 34}})
Group = ['Group1','Group2','Group3','Group4']
pd.concat([pd.DataFrame(Group),df], axis=1)

df being your dataframe and Group being your list.

Upvotes: 2

Related Questions