PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

[FIXED] How do I use pandas.shift() without losing data?

 August 29, 2022     csv, pandas, python     No comments   

Issue

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

Solution

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


Answered By - BeRT2me
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing