In this tutorial, We create a series of 12 elements, random integers from 7000-10000, representing monthly income and set the index to be the month names, starting in January and ending in December.

Create Random Integers

First, we create a series of 12 elements, with random integers from 7000-10000. To define a Pandas series, we call Series, passing it an iterable—typically, a Python list or NumPy array.  

We can get a NumPy array of random integers by calling np.random.randint() it’s three arguments to indicate the range (minimum and maximum) of the random numbers and how many we want.

np.random.randint(7000, 10000, 12)

First, the third argument indicates the length of the returned series. Second, the second argument is one more than the highest value we can get back. I can use them to create a series: 

s = Series(np.random.randint(7000, 10000, 12))
Pandas Random Integer

Create Index

We now have a series of random integers between 7000 and 10000. But the index contains integers from 0 through 11—much as would be the case in a NumPy array. 

There’s nothing inherently wrong with a numeric index, but Pandas gives us much more power and flexibility, letting us use a wide variety of data types, including strings. 

We can change the index by assigning to the index attribute:

s.index = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec '.split()

Pandas Series Set Index

You can assign a list, NumPy array, or Pandas series as an index. However, the data structure you pass must be of the same length as the series. If it isn’t, you’ll get a ValueError exception, and the assignment will fail. 

If we know what index we’ll want when we create the series, we can assign it to the index keyword parameter:

months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec '.split()

s = pd.Series(np.random.randint(7000, 10000, 12),index=months)

This is the preferred method for creating a series. That said, if and when I ever want to change the index, I can do that by assigning a new value to s.index

Pandas typically work with data in two-dimensional tables, known as “data frames,” with rows and columns. But each column in a data frame is built from a “series,” a one-dimensional data structure, which means that you can think of a data frame as a collection of series. 

To convert Pandas Series to DataFrame you can pass Series as params within a dict using the DataFrame constructor:

income_df=pd.DataFrame({'month':s.index, 'income':s.values})

Pandas Dreate DataFrame With Random Data

Related Post

Filter Pandas Dataframe using OR(|) AND(&) with Query()

Discretization, Binning, and Count in Pandas Column.

Detect and Remove Outliers from Pandas DataFrame

Scaling Pandas DataFrame with MinMaxScaler

Standardize Pandas DataFrame Using StandardScaler

Normalize, Scale, and Standardize Pandas DataFrame columns using Scikit-Learn