Skip to main content

Building and Organizing DataFrames

Each function/method creates a new DataFrame. Remember to save it!

Manipulating


Creates an empty DataFrame.

bpd.read_csv(filepath)

Reads a CSV (comma-separated values) file into a DataFrame.

df.assign(name_of_column=column_data)

Adds a new column to the DataFrame.

df.drop(columns=column_name or [col_1_name, ..., col_k_name])

Drops a single column, or every column in a list of column names, from the DataFrame.

Indexing


df.set_index(column_name)

Moves a column to the DataFrame's index.

Organizing


df.reset_index(drop=False)

Moves the index to a new column and uses the default index instead.

df.sort_values(by=column_name, ascending=True)

Sorts the entire DataFrame in ascending order by the values in the column.

Merging


df.merge(right, how='inner', on=column, left_on=left_column, right_on=right_column, left_index=False, right_index=False)

Merges two DataFrames by specified columns or indexes, using the specified type of merge.