jekwatt / idiomatic_pandas

Tips and tricks for the most common data handling task with pandas.
0 stars 0 forks source link

Add, remove rows and columns from DataFrames #17

Open jekwatt opened 3 years ago

jekwatt commented 3 years ago

Add columns:

df["col_3"] = df["col_1"] + " " + df["col2"]
df[["col_1", "col_2"]] = df["col_3"].str.split(" ", expand=True)

Remove columns: df.drop(columns=["col_1", "col_2"], inplace=True)

jekwatt commented 3 years ago

Add a single row of data:

d1 =  {k1: v1}
df.append(d1, ignore_index=True)  # conflicting index

Add rows:

# conflicting index, fields not in the same order
# future version of Pandas will set the sort to False by default
df.append(df2, ignore_index=True, sort=False)

# unlike drop method, don't have inplace method to use
df = df.append(df2, ignore_index=True, sort=False)
jekwatt commented 3 years ago

Remove a row: df.drop(index=4)

Remove rows using conditional:

mask = (df["col_1"] == v1)
df.drop(index=df[mask].index)
jekwatt commented 3 years ago

These will go under notebooks/03_modifying.