Pandas - Manipulation data column-wise

import pandas as pd

iris = pd.read_csv("archive.ics.uci.edu/ml/machine-learning-dat..")

df = iris.copy()

df.columns = ['sl', 'sw', 'pl', 'pw', 'flower_type']

1) deleting a column

/ Method 1

df.drop('sl', axis = 1, inplace = True)

print(df)

swplpwflower_type
03.01.40.2Iris-setosa
13.21.30.2Iris-setosa
23.11.50.2Iris-setosa
33.61.40.2Iris-setosa
43.91.70.4Iris-setosa
...............
1443.05.22.3Iris-virginica
1452.55.01.9Iris-virginica
1463.05.22.0Iris-virginica
1473.45.42.3Iris-virginica
1483.05.11.8Iris-virginica

149 rows × 4 columns

Here, axis denotes to check it in column wise i.e., axis = 1 -> column_number = 1.

inplace = True, if not used it won't perform any changes in df instead creates a copy of it and performs the action in the copy.

/ Method 2

del df['sw']

print(df)

plpwflower_type
01.40.2Iris-setosa
11.30.2Iris-setosa
21.50.2Iris-setosa
31.40.2Iris-setosa
41.70.4Iris-setosa
............
1445.22.3Iris-virginica
1455.01.9Iris-virginica
1465.22.0Iris-virginica
1475.42.3Iris-virginica
1485.11.8Iris-virginica

149 rows × 3 columns

2) adding a column

df["diff_pl_pw"] = df["pl"] - df["pw"]

print(df)

plpwflower_typediff_pl_pw
01.40.2Iris-setosa1.2
11.30.2Iris-setosa1.1
21.50.2Iris-setosa1.3
31.40.2Iris-setosa1.2
41.70.4Iris-setosa1.3
...............
1445.22.3Iris-virginica2.9
1455.01.9Iris-virginica3.1
1465.22.0Iris-virginica3.2
1475.42.3Iris-virginica3.1
1485.11.8Iris-virginica3.3

149 rows × 4 columns

You can also do this but make sure the no. of entries is equal to the row count.

Eg:- df["sl"] = [1, 2, 3,......, 148] / the dots must be replaced with values.