LearnWave
Learn it. Master it.

Data Handling using Pandas - I

Creating a DataFrame

Create DataFrames and predict their rows, columns and NaN.

A DataFrame is a two-dimensional labelled data structure, like a table: it has a row index and column labels. Each column is a Series, and different columns can hold different data types. pd.DataFrame() with no data gives an empty DataFrame.

Ways to create a DataFrame:

  • from NumPy arrays: each array becomes a row; a shorter array is padded with NaN;
  • from a list of dictionaries: the keys become columns, and each dictionary becomes a row; a missing key gives NaN;
  • from a dictionary of lists: the keys are the column labels, and each list is a column. The columns= parameter can set the order of the columns;
  • from a dictionary of Series: the row labels are the union of all the Series' indexes; missing values become NaN.

Example: pd.DataFrame({'Name': ['Asha', 'Ravi'], 'Marks': [88, 92]}) makes a table with 2 rows and the columns Name and Marks. Dictionary keys become columns.

Dictionary keys become columns.
Dictionary keys become columns.

Practise this lesson

Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.

No account, no email address. Opens the real product.