Write a program in Python to shift the first column and get the value from the user, if the input is divisible by both 3 and 5 and then fill the missing value


Input

Assume you have a DataFrame, and the result for shifting the first column and fill the missing values are,

 one two three
0 1   10 100
1 2   20 200
2 3   30 300
enter the value 15
 one two three
0 15  1   10
1 15  2   20
2 15  3   30

Solution

To solve this, we will follow the below approach.

  • Define a DataFrame

  • Shift the first column using below code,

data.shift(periods=1,axis=1)
  • Get the value from user and verify if it is divisible by 3 and 5. If the result is true then fill missing value, otherwise fill NaN. It is defined below,

user_input = int(input("enter the value"))
if(user_input%3==0 and user_input%5==0):
   print(data.shift(periods=1,axis=1,fill_value=user_input))
else:
   print(data.shift(periods=1,axis=1))

Example

Let us see the complete implementation to get better understanding −

import pandas as pd
data= pd.DataFrame({'one': [1,2,3],
                     'two': [10,20,30],
                     'three': [100,200,300]})
print(data)
user_input = int(input("enter the value"))
if(user_input%3==0 and user_input%5==0):
   print(data.shift(periods=1,axis=1,fill_value=user_input))
else:
   print(data.shift(periods=1,axis=1))

Output 1

 one two three
0 1   10   100
1 2   20   200
2 3   30   300
enter the value 15
 one two three
0 15   1 10
1 15   2 20
2 15   3 30

Output 2

one two three
0    1    10 100
1    2    20 200
2    3    30 300
enter the value 3
  one two three
0 NaN 1.0 10.0
1 NaN 2.0 20.0
2 NaN 3.0 30.0

Updated on: 24-Feb-2021

24 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements