Issue
I have a dataframe like this:
John
30
Mike
0.0786268
Tyson
0.114889
Gabriel
0.176072
Fiona
0.101895
I need to shift every second row to a new column so it should look like this:
John 30
Mike 0.0786268
Tyson 0.114889
Gabriel 0.176072
Fiona 0.101895
Solution
This one works for me and the code is easy to understand:
names = []
numbers = []
for i in range(len(df['Names'])):
if (i % 2) == 0:
names.append(df['Names'][i])
else:
numbers.append(df['Names'][i])
Now the construction of the dataframe:
new_df = pd.DataFrame([names, numbers]).T
new_df.columns = ['Names', 'Numbers']
new_df
The Output:
Names Numbers
0 John 30
1 Mike 0.0786268
2 Tyson 0.114889
3 Gabriel 0.176072
4 Fiona 0.101895
Answered By - Luis Alejandro Vargas Ramos
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.