Copy corresponding dataframe column values to new dataframe with fixed column names python


This Content is from Stack Overflow. Question asked by Pradyumna TK

I have a data frame (df1) as shown below. The elements in the 1st column start repeating after a certain number but the corresponding 2nd column elements are unique

    0        1
0   A        Ok
1   B        1234
2   C        True
3   D        Yes
4   A        Ok
5   B        6789
6   C        False
7   D        No 

I have created a new DataFrame(df2) with column names as the elements in the 1st column of df1 without repetition

column_names = df1[0].drop_duplicates()
df2 = pd.DataFrame(columns=column_names)

I want to copy the elements in the 2nd column of df1 under the column names in df2 as shown

    A     B       C      D
0   Ok   1234    True    Yes
1   Ok   6789    False   Yes
2   Ok   1567    False   N/A

Can anyone help me with this?


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?