Home > Enterprise >  Python shift data on every second row from one column to a new column
Python shift data on every second row from one column to a new column

Time:08-22

Newbie to python here...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

Thanks in advanced!

CodePudding user response:

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

CodePudding user response:

another method that you can use by using subset

data=StringIO("""data
John
30
Mike
0.0786268
Tyson
0.114889
Gabriel
0.176072
Fiona
0.101895""")
old_df = pd.read_csv(data, sep=";")

new_df=pd.DataFrame({'name': old_df.loc[(old_df.index%2)==0,'data'].to_numpy(), 'value': old_df.loc[(old_df.index%2)==1,'data'].to_numpy()})

The result

    name    value
0   John    30
1   Mike    0.0786268
2   Tyson   0.114889
3   Gabriel 0.176072
4   Fiona   0.101895

CodePudding user response:

You can use divmod() to create a new index

df.set_index(list(divmod(df.index,2)))['Col'].unstack().set_axis(['Names','Numbers'],axis=1)

     Names    Numbers
0     John         30
1     Mike  0.0786268
2    Tyson   0.114889
3  Gabriel   0.176072
4    Fiona   0.101895

CodePudding user response:

Assuming df['col'] your column, a simple and efficient method would be to use the underlying numpy array to reshape:

out = pd.DataFrame(df['col'].values.reshape(-1,2),
                   columns=['Name', 'Value'])

Output:

      Name      Value
0     John         30
1     Mike  0.0786268
2    Tyson   0.114889
3  Gabriel   0.176072
4    Fiona   0.101895
  • Related