Home > Net >  Data manipulation using pandas function
Data manipulation using pandas function

Time:03-11

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?

CodePudding user response:

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
  • Related