-1

[[https://github.com/Shristigithub/Population/blob/master/population.csv]]1 [https://i.stack.imgur.com/Bf64A.png]2

Please help me to get one country name and all the years columns separately.

i want output to look something like this

enter image description here

2 Answers2

0

Is this what you want:

import pandas as pd

df = pd.DataFrame({
  'id':['a','a','b','c','c'], 
  'words':['asd','rtr','s','rrtttt','dsfd']})
print(df)

zet = df.groupby('id')['words'].apply(','.join)
print(zet)

Output:

  id   words
0  a     asd
1  a     rtr
2  b       s
3  c  rrtttt
4  c    dsfd


id
a        asd,rtr
b              s
c    rrtttt,dsfd
Name: words, dtype: object

In your case maybe something like this:

sorted_output = df.groupby('coutry_name')['years'].apply(','.join)
Scott
  • 4,974
  • 6
  • 35
  • 62
0

Try the below code, hope this will help.

Consider your data frame to be like this.

df = pd.DataFrame({'Date': [1,2,34,6,7,8,9],'values':[123,3454,6734,123,45,234,123]})

So df will be like:

    Date    values
0     1     123
1     2     3454
2     34    6734
3     6     123
4     7     45
5     8     234
6     9     123

Now first take transpose and then set column value, as shown below:

df = df.transpose()
df.columns= df.iloc[0]
df

Ouput will be:

Date    1        2       34     6        7    8     9    <--- These are now columns headers
Date    1        2       34     6        7    8     9       
values  123     3454    6734    123     45  234     123
Shishir Naresh
  • 743
  • 4
  • 10