To delete all columns except one in a pandas DataFrame, you can use the drop method with the column names you want to remove. Here's an example:
import pandas as pd # Sample DataFrame data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]} df = pd.DataFrame(data) # Keep only one column (e.g., 'A') and drop the rest columns_to_keep = ['A'] df = df[columns_to_keep] print(df) In this example, we first create a sample DataFrame df with columns 'A', 'B', and 'C'. To keep only column 'A' and delete the rest, we specify 'A' in the columns_to_keep list and then assign the filtered DataFrame back to df. The resulting DataFrame will contain only the 'A' column.
If you want to keep a column other than 'A', simply replace 'A' with the name of the column you want to retain in the columns_to_keep list.
Delete all but one column of Pandas DataFrame using indexing:
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # Keep only column 'A' df = df[['A']] Drop all but one column from Pandas DataFrame using drop() method:
drop() method to eliminate all columns except the specified one from a Pandas DataFrame.import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # Drop all columns except 'A' df.drop(columns=['B', 'C'], inplace=True) Remove all but one column from Pandas DataFrame with slice assignment:
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # Keep only column 'A' df = df[['A']] Delete all but one column of Pandas DataFrame with loc accessor:
loc accessor to select and keep only one column in a Pandas DataFrame.import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # Keep only column 'A' df = df.loc[:, ['A']] Dropping all but one column of Pandas DataFrame with iloc accessor:
iloc accessor to remove all columns except one from a Pandas DataFrame.import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # Keep only column 'A' df = df.iloc[:, [0]] Remove all but one column from Pandas DataFrame by column index:
import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # Keep only column at index 0 ('A') df = df.iloc[:, [0]] Deleting all but one column of Pandas DataFrame with filter() method:
filter() method to retain only the specified column in a Pandas DataFrame, discarding all others.import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # Keep only column 'A' df = df.filter(['A']) Drop all but one column from Pandas DataFrame using reindex() method:
reindex() method to select and keep only one column in a Pandas DataFrame.import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # Keep only column 'A' df = df.reindex(columns=['A']) Removing all but one column of Pandas DataFrame using pop() method:
pop() method to remove all columns except the specified one from a Pandas DataFrame.import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # Keep only column 'A' df.pop('A') Delete all but one column of Pandas DataFrame with drop() method and axis parameter:
drop() method with the axis parameter set to 1 to remove all columns except the specified one from a Pandas DataFrame.import pandas as pd # Create a DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # Keep only column 'A' df.drop(['B', 'C'], axis=1, inplace=True) mapfragment homekit typescript getstring oracle12c java-11 entities fetch-api ng-class spotify