add an Index, row, or column to a Pandas DataFrame?
To add an index, row, or column to a Pandas DataFrame, you can use any of the following approaches:
- Adding an index:
You can add an index to a Pandas DataFrame by assigning a list of labels to the index property of the DataFrame, like this:
import pandas as pd
df = pd.DataFrame({'Name': ['John', 'Mike', 'Sarah'], 'Age': [25, 32, 28]})
df.index = ['A', 'B', 'C']
print(df)
Output:
Name Age
A John 25
B Mike 32
C Sarah 28
- Adding a row:
You can add a row to a Pandas DataFrame by appending a new row as a dictionary to the DataFrame, like this:
import pandas as pd
df = pd.DataFrame({'Name': ['John', 'Mike', 'Sarah'], 'Age': [25, 32, 28]})
new_row = {'Name': 'Tom', 'Age': 30}
df = df.append(new_row, ignore_index=True)
print(df)
Output:
Name Age
0 John 25
1 Mike 32
2 Sarah 28
3 Tom 30
- Adding a column:
You can add a column to a Pandas DataFrame by assigning a list of values to a new column label, like this:
import pandas as pd
df = pd.DataFrame({'Name': ['John', 'Mike', 'Sarah'], 'Age': [25, 32, 28]})
df['Gender'] = ['Male', 'Male', 'Female']
print(df)
Output:
Name Age Gender
0 John 25 Male
1 Mike 32 Male
2 Sarah 28 Female
In each of the above examples, we modify the DataFrame by assigning a new value to the index, columns, or adding a new row as a dictionary. Note that appending a row to a DataFrame returns a new DataFrame, so it is necessary to assign the result back to the original variable to modify the DataFrame in place.