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

This article demonstrates how to shift DataFrame columns and conditionally fill missing values based on user input. We'll shift the first column to the right and fill the new empty column with a user-provided value only if it's divisible by both 3 and 5.

Understanding Column Shifting

The shift() method with axis=1 shifts columns horizontally. When shifting right, the first column becomes empty and needs to be filled.

Creating the DataFrame

import pandas as pd

data = pd.DataFrame({
    'one': [1, 2, 3],
    'two': [10, 20, 30],
    'three': [100, 200, 300]
})
print("Original DataFrame:")
print(data)
Original DataFrame:
   one  two  three
0    1   10    100
1    2   20    200
2    3   30    300

Shifting Columns with Conditional Fill

We'll get user input and check if it's divisible by both 3 and 5 (i.e., divisible by 15) before filling the shifted column ?

import pandas as pd

data = pd.DataFrame({
    'one': [1, 2, 3],
    'two': [10, 20, 30],
    'three': [100, 200, 300]
})

print("Original DataFrame:")
print(data)
print()

# Simulate user input (normally you'd use input())
user_input = 15  # Example: divisible by both 3 and 5

print(f"User input: {user_input}")

if user_input % 3 == 0 and user_input % 5 == 0:
    print("Input is divisible by both 3 and 5 - filling with user value:")
    shifted_data = data.shift(periods=1, axis=1, fill_value=user_input)
else:
    print("Input is NOT divisible by both 3 and 5 - leaving NaN:")
    shifted_data = data.shift(periods=1, axis=1)

print(shifted_data)
Original DataFrame:
   one  two  three
0    1   10    100
1    2   20    200
2    3   30    300

User input: 15
Input is divisible by both 3 and 5 - filling with user value:
   one  two  three
0   15    1     10
1   15    2     20
2   15    3     30

Example with Non-Divisible Input

import pandas as pd

data = pd.DataFrame({
    'one': [1, 2, 3],
    'two': [10, 20, 30],
    'three': [100, 200, 300]
})

# Example with input not divisible by both 3 and 5
user_input = 7

print(f"User input: {user_input}")

if user_input % 3 == 0 and user_input % 5 == 0:
    print("Filling with user value:")
    shifted_data = data.shift(periods=1, axis=1, fill_value=user_input)
else:
    print("Not divisible by both 3 and 5 - leaving NaN:")
    shifted_data = data.shift(periods=1, axis=1)

print(shifted_data)
User input: 7
Not divisible by both 3 and 5 - leaving NaN:
   one  two  three
0  NaN  1.0   10.0
1  NaN  2.0   20.0
2  NaN  3.0   30.0

Key Parameters

The shift() method accepts these important parameters ?

  • periods=1 − Number of positions to shift
  • axis=1 − Shift columns (axis=0 shifts rows)
  • fill_value − Value to fill the new empty positions

Conclusion

Use shift(axis=1) to move DataFrame columns horizontally. Combine with conditional logic to fill missing values based on specific criteria like divisibility rules.

Updated on: 2026-03-25T16:14:55+05:30

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements