How to create a sample dataframe quickly using pandas and numpy

 Sometimes, we just want to try something on a dataframe and we don't have any other dataframe available. In this situation, we can quickly create a dataframe sample using pandas pd.dataframe function and we can add some random data into this dataframe using numpy's np.rand function.

First we import both pandas and numpy libraries


Using pandas alone...

df = pd.DataFrame({'apple':[100,200,300],'orange':[400,500,600]})

df




Using pandas and numpy both...

To make more rows and columns, just provide under rand function as 

rand(rows,columns) and names of columns under list(column names)

df1 = pd.DataFrame(np.random.rand(10,8),columns=list('ABCDEFGH'))

df1





No comments:

Post a Comment

Complex query example

Requirement:  You are given the table with titles of recipes from a cookbook and their page numbers. You are asked to represent how the reci...