Answer to Question #310715 in Python for Akash Singh

Question #310715

Problem Statement:

You work in XYZ Company as a Python. The company officials want you to build a python program. 

1. Write a function that takes start and end of a range returns a Pandas series object containing numbers within that range. In case the user does not pass start or end or both they should default to 1 and 10 respectively. eg. range_series()

-> Should Return a pandas series from 1 to 10 range_series(5)

-> Should Return a pandas series from 5 to 10 range_series(5, 10)

-> Should Return a pandas series from 5 to 10. 


2. Create a function that takes in two lists named keys and values as arguments. Keys would be strings and contain n string values. Values would be a list containing n lists.

The methods should return a new pandas dataframe with keys as column names and values as their corresponding values e.g.

-> create_dataframe(["One", "Two"], [["X", "Y"], ["A", "B"]]) -

> should return a dataframe One Two 0 X A 1 Y B 


1
Expert's answer
2022-03-13T15:11:32-0400
import pandas as pd
def range_series(start=1, end=10):
    df = pd.Series(range(start,end))
    return df
range_series(5)

0    5
1    6
2    7
3    8
4    9
dtype: int64

def create_dataframe(keys, values):
    dic = {}
    for i in range(len(keys)):
        dic[keys[i]] = values[i]
    df = pd.DataFrame(dic)
    return df
create_dataframe(["One", "Two"], [["X", "Y"], ["A", "B"]]) 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS