- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- Write a program in Python to create a panel from a dictionary of dataframe and print the maximum value of the first column
- Write a program in Python to find the lowest value in a given DataFrame and store the lowest value in a new row and column
- Swift Program to Get Input from the User
- Java Program to Get Input from the User
- Haskell program to get input from the user
- Find the smallest value of a, if the number(6a4231) is divisible by 3.
- Write the smallest number which is divisible by both 306 and 657.
- If the points $( a, b), ( 3, -5)$ and $( -5, -2)$ are collinear. Then find the value of $3a+8b$.
- Python program to print all the numbers divisible by 3 and 5 for a given number
- Program to print all the numbers divisible by 3 and 5 in C++
- Python Program to Swap the First and Last Value of a List
- If a point $A (0, 2)$ is equidistant from the points $B (3, p)$ and $C (p, 5)$, then find the value of $p$.
- If ( angle 5: angle 6=3: 2 ), then find the value of ∠5 and ∠6."
- If the distance between the points ( (x,-1) ) and ( (3,2) ) is 5 , then the value of $x$ is
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?

Advertisements