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)

Read a comma-separated values (csv) file into 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.

Indexing


df.set_index(column_name)

Moves a column to the index.

Organizing


df.reset_index(drop=False)

Moves a column to the index.

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.