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)
sw | pl | pw | flower_type | |
0 | 3.0 | 1.4 | 0.2 | Iris-setosa |
1 | 3.2 | 1.3 | 0.2 | Iris-setosa |
2 | 3.1 | 1.5 | 0.2 | Iris-setosa |
3 | 3.6 | 1.4 | 0.2 | Iris-setosa |
4 | 3.9 | 1.7 | 0.4 | Iris-setosa |
... | ... | ... | ... | ... |
144 | 3.0 | 5.2 | 2.3 | Iris-virginica |
145 | 2.5 | 5.0 | 1.9 | Iris-virginica |
146 | 3.0 | 5.2 | 2.0 | Iris-virginica |
147 | 3.4 | 5.4 | 2.3 | Iris-virginica |
148 | 3.0 | 5.1 | 1.8 | Iris-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)
pl | pw | flower_type | |
0 | 1.4 | 0.2 | Iris-setosa |
1 | 1.3 | 0.2 | Iris-setosa |
2 | 1.5 | 0.2 | Iris-setosa |
3 | 1.4 | 0.2 | Iris-setosa |
4 | 1.7 | 0.4 | Iris-setosa |
... | ... | ... | ... |
144 | 5.2 | 2.3 | Iris-virginica |
145 | 5.0 | 1.9 | Iris-virginica |
146 | 5.2 | 2.0 | Iris-virginica |
147 | 5.4 | 2.3 | Iris-virginica |
148 | 5.1 | 1.8 | Iris-virginica |
149 rows × 3 columns
2) adding a column
df["diff_pl_pw"] = df["pl"] - df["pw"]
print(df)
pl | pw | flower_type | diff_pl_pw | |
0 | 1.4 | 0.2 | Iris-setosa | 1.2 |
1 | 1.3 | 0.2 | Iris-setosa | 1.1 |
2 | 1.5 | 0.2 | Iris-setosa | 1.3 |
3 | 1.4 | 0.2 | Iris-setosa | 1.2 |
4 | 1.7 | 0.4 | Iris-setosa | 1.3 |
... | ... | ... | ... | ... |
144 | 5.2 | 2.3 | Iris-virginica | 2.9 |
145 | 5.0 | 1.9 | Iris-virginica | 3.1 |
146 | 5.2 | 2.0 | Iris-virginica | 3.2 |
147 | 5.4 | 2.3 | Iris-virginica | 3.1 |
148 | 5.1 | 1.8 | Iris-virginica | 3.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.