Pandas FAQ

Click on the links in the headings for more information.

Create empty dataframe

df_empty = pd.DataFrame()

Creating & editing entries

# Replacing pandas dataframe column values with another value.
# Values to replace = ['ABC', 'AB']
# Replacement value = 'A'
df['BrandName'] = df['BrandName'].replace(['ABC', 'AB'], 'A')

# Turn off warnings where overwriting dataframe values.
pd.options.mode.chained_assignment = None  # default='warn'

Concatenating dataframes

df_empty = pd.DataFrame()

Renaming headers

# Provide a dictionary with "before":"after" names of the items to be renamed.
df.rename(columns={"A": "a", "B": "c"})

Concatenating

# Concatenating dataframes by row, ie appending rows with the same header.
result = df1.append(df4, sort=False)
# or
result = pd.concat([df1, df4], axis=0, sort=False)

# Concatenating dataframes by column, ie appending additional header columns.
result = pd.concat([df1, df4], axis=1, sort=False)

This may require reindexing each dataframe that needs to be appended - see this Concatenating for more information.

Find number of rows in dataframe

len(df)

Indexing data

df.loc[row_indexer,column_indexer]