Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Data manipulation using pandas function

I want to write a function that takes start and end of a range and returns a Pandas series object containing the numbers within that range. But I need to know how to run the function with pre-determined procedure when no arguments are given.

Here if I put:

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

How can this be done?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

import pandas as pd

def func(a = 1, b = 10):
    c = []
    for i in range(a,b+1):
        c.append(i)
    return pd.Series(data=c)
    
print(func())
print(func(4,10))

This what you want?
output:

0     1
1     2
2     3
3     4
4     5
5     6
6     7
7     8
8     9
9    10
dtype: int64
0     4
1     5
2     6
3     7
4     8
5     9
6    10
dtype: int64
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading