0

I have a pandas data frame with many rows and columns like this

Name        Skill       Age
Adam        C++         23
Beth        Java        25
Micheal     Scala       21
...
Aaron       Erlang      23

I have another list from which i can create a pandas series

dept = ['Country', 'UK']
pd.Series[dept]
s = pd.Series(dept)

Now i want to concat the dataframe and the Series with Second element of the list should be repeated.

Name        Skill       Age         Country
Adam        C++         23          UK
Beth        Java        25          UK
Micheal     Scala       21          UK
...
Aaron       Erlang      23          UK

UK should be repeated and the Country should become the label for the series.

I am clueless on how to achieve this

jhon.smith
  • 1,963
  • 6
  • 30
  • 56

1 Answers1

1

Select values of list by indexing for column name and for values:

dept = ['Country', 'UK']

df[dept[0]] = dept[1]
print (df)
      Name   Skill  Age Country
0     Adam     C++   23      UK
1     Beth    Java   25      UK
2  Micheal   Scala   21      UK
3    Aaron  Erlang   23      UK

If input data is Series select by position by Series.iat:

s = pd.Series(dept)
df[s.iat[0]] = s.iat[1]
#if default RangeIndex
#df[s[0]] = s[1]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252