Home > Back-end >  How do I use pandas.shift() without losing data?
How do I use pandas.shift() without losing data?

Time:06-26

I am trying to shift certain rows in a .csv down without losing the last row. Say if you use df.shift(1), it will return the shifted dataframe, removing the last row of data. What I'm wanting is it to shift without losing the last row of data.

Original (data):

example1,example1
example2,example2
example3,example3

What happens after data.shift(1):

NaN,NaN
example1,example1
example2,example2

What I would like:

NaN,NaN
example1,example1
example2,example2
example3,example3

Maybe creating a row on the end filled with NaN would fix this? (I don't know how to fill cells with NaN).

Here is my actual code where I am selecting certain rows:

import pandas
data = pandas.read_csv('path/test.csv', header=False,
yes = data.iloc[2:3].shift(1)
print(yes)

output:

NaN,NaN
example2,example2

CodePudding user response:

Starting with:

          0         1
0  example1  example1
1  example2  example2
2  example3  example3

Doing:

import pandas as pd
import numpy as np

# Version 1:
df.index  = 1
df.loc[0] = np.nan
df = df.sort_index()

# Version 2:
df.loc[-1] = np.nan
df = df.sort_index().reset_index(drop=True)

Output:

          0         1
0       NaN       NaN
1  example1  example1
2  example2  example2
3  example3  example3

CodePudding user response:

Taking the approach of adding a row:

df = pd.DataFrame([['example1','example1'],['example2','example2'],['example3','example3']])
row = ['blank','blank']

df.loc[len(df)] = row

Gives:

    0   1
0   example1    example1
1   example2    example2
2   example3    example3
3   blank   blank
  • Related