1.
Adding or Modifying Columns
When adding a new column to a pandas DataFrame, you can use the following methods:
Same value for all rows
Different values for each row
Using ‘at’
Using ‘loc’
Using ‘assign()’
import pandas as pd
data = {"A":[1,2,3], "B":[4,5,6]}
df = pd.DataFrame(data)
print(df)
df["C"] = 7 # Same value for all rows
df["D"] = [11,12,13] # Different values for each row
df.at[:,"E"] = [21,22,23] # using at
df.loc[:,"F"] = [32,33,34] # using loc
df = df.assign(G = [41,42,43]) #using assign()
print(df)
In pandas you typically use loc to add rows to a DataFrame, while at is primarily used for
accessing or modifying a single value at a specific row and column and is not used for adding
rows.
import pandas as pd
data = {"A":[1,2,3], "B":[4,5,6]}
df = pd.DataFrame(data)
print(df)
df.loc[len(df)] = [21,22] # adding a row to the end using loc
df.loc[4] = [32,35] # adding a row at a specific index using loc
df.at[1,"B"] = 50 # modifying a single value using at
print(df)
2. Adding multiple rows
Adding multiple rows to a pandas DataFrame can be done using following methods:
Using concat()
Using _append()
import pandas as pd
data = {"A":[1,2,3], "B":[4,5,6]}
df = pd.DataFrame(data)
print(df)
df2 = pd.DataFrame({"A":[21,22,23], "B":[31,32,33]})
print(df2)
df = df._append(df2, ignore_index = True)
df = pd.concat([df,df2], ignore_index=True)
print("\n\n",df)
3. Modifying a single cell
Modifying a single cell in a DataFrame can be done using the loc, at, iloc, iat accessors.
import pandas as pd
data = {"A":[1,2,3], "B":[4,5,6]}
df = pd.DataFrame(data)
print(df)
df.loc[1,"A"] = 20 # row, column
df.at[1,"B"] = 30 # row, column
df.iloc[2,0] = 40 # row, column
df.iat[2,1] = 50 # row, column
print("\n\n",df)
4. Deleting rows/ columns
To delete a column:
To delete a column, you need to use the del statement, for example
Del df[“A”]
To delete rows:
To deleting rows form a DataFrame you can use drop(index or sequence of indexes)
Df = df.drop(1)
Df = df.drop([1,2])
import pandas as pd
data = {"A":[500,200,300,450,100,900], "B":[44,55,66,77,88,99]}
df = pd.DataFrame(data)
print(df)
del df["A"] # for a single column
print("\n\n",df)
df = df.drop(1) # for a single row
df = df.drop([0,2]) # for multiple rows
print("\n\n",df)
5. Conditions
import pandas as pd
data = {"A":[500,200,300,450,100,900], "B":[44,55,66,77,88,99]}
df = pd.DataFrame(data)
print(df)
print(df["A"]>=400)
df = df[df["A"]>=400]
print("\n\n",df)
6. Renaming rows/columns
Renaming columns
To rename columns, you can use the “rename” method or directly modify the “columns” attribute of
the DataFrame.
import pandas as pd
data = {"A":[1,2,3], "B":[4,5,6]}
df = pd.DataFrame(data)
print(df)
df.rename(columns={"A": "Alpha","B":"Beta"}, inplace=True)
df = df.rename(columns = {"A": "Apple", "B":"Banana"})
df.columns = ["Ark","Boss"]
print("\n\n",df)
Renaming rows
Renaming rows involves changing the index of the DataFrame. You can also use the rename
method for this, or directly modify the index attribute
import pandas as pd
data = {"A":[1,2,3], "B":[4,5,6]}
df = pd.DataFrame(data, index=['a','b','c'])
print(df)
df.rename(index={"a":"row1","b":"row2","c":"row3"}, inplace=True)
df = df.rename(index={"a":"apple","b":"mango","c":"banana"})
df.index = ["row1","row2","row3"]
print("\n\n",df)