Select multiple ranges of columns in Pandas DataFrame

Select multiple ranges of columns in Pandas DataFrame

You can select multiple ranges of columns from a Pandas DataFrame by using the .iloc[] indexer along with the column index ranges. Here's how you can do it:

Let's say you have a DataFrame named df and you want to select multiple ranges of columns:

import pandas as pd data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9], 'D': [10, 11, 12], 'E': [13, 14, 15]} df = pd.DataFrame(data) # Select columns from index 1 to 2 (inclusive) cols_range1 = df.iloc[:, 1:3] # Select columns from index 3 to 4 (inclusive) cols_range2 = df.iloc[:, 3:5] # Concatenate the selected column ranges selected_columns = pd.concat([cols_range1, cols_range2], axis=1) print(selected_columns) 

Output:

 B C D E 0 4 7 10 13 1 5 8 11 14 2 6 9 12 15 

In this example, df.iloc[:, 1:3] selects columns from index 1 to 2 (inclusive), and df.iloc[:, 3:5] selects columns from index 3 to 4 (inclusive). You can adjust the column index ranges based on your specific requirements.

The pd.concat() function is used to concatenate the selected column ranges along the axis=1 (columns) to create the final DataFrame with the desired columns.

Examples

  1. How to select multiple ranges of columns in Pandas DataFrame using iloc[]?

    # Description: This query demonstrates selecting multiple ranges of columns in Pandas DataFrame using iloc[]. import pandas as pd # Assuming 'df' is your Pandas DataFrame selected_columns = df.iloc[:, [0:3, 5:8]] 
  2. Selecting multiple ranges of columns in Pandas DataFrame with loc[]

    # Description: This query illustrates selecting multiple ranges of columns in Pandas DataFrame with loc[]. import pandas as pd # Assuming 'df' is your Pandas DataFrame selected_columns = df.loc[:, ['column1':'column3', 'column5':'column7']] 
  3. How to select multiple ranges of columns in Pandas DataFrame using index slicing?

    # Description: This query showcases selecting multiple ranges of columns in Pandas DataFrame using index slicing. import pandas as pd # Assuming 'df' is your Pandas DataFrame selected_columns = df.iloc[:, list(range(0, 3)) + list(range(5, 8))] 
  4. Selecting multiple ranges of columns in Pandas DataFrame with iloc[] and integer list

    # Description: This query demonstrates selecting multiple ranges of columns in Pandas DataFrame with iloc[] and integer list. import pandas as pd # Assuming 'df' is your Pandas DataFrame selected_columns = df.iloc[:, [0, 1, 2, 5, 6, 7]] 
  5. How to use loc[] to select multiple ranges of columns in Pandas DataFrame?

    # Description: This query illustrates using loc[] to select multiple ranges of columns in Pandas DataFrame. import pandas as pd # Assuming 'df' is your Pandas DataFrame selected_columns = df.loc[:, 'column1':'column3'].join(df.loc[:, 'column5':'column7']) 
  6. Selecting multiple ranges of columns in Pandas DataFrame with loc[] and column names

    # Description: This query showcases selecting multiple ranges of columns in Pandas DataFrame with loc[] and column names. import pandas as pd # Assuming 'df' is your Pandas DataFrame selected_columns = df.loc[:, df.columns[0:3].tolist() + df.columns[5:8].tolist()] 
  7. How to select multiple ranges of columns in Pandas DataFrame using index slicing with loc[]?

    # Description: This query demonstrates selecting multiple ranges of columns in Pandas DataFrame using index slicing with loc[]. import pandas as pd # Assuming 'df' is your Pandas DataFrame selected_columns = df.loc[:, df.columns[0:3].append(df.columns[5:8])] 
  8. Selecting multiple ranges of columns in Pandas DataFrame with reindex()

    # Description: This query illustrates selecting multiple ranges of columns in Pandas DataFrame with reindex(). import pandas as pd # Assuming 'df' is your Pandas DataFrame selected_columns = df.reindex(columns=df.columns[0:3].append(df.columns[5:8])) 
  9. How to select multiple ranges of columns in Pandas DataFrame using index slicing with iloc[]?

    # Description: This query showcases selecting multiple ranges of columns in Pandas DataFrame using index slicing with iloc[]. import pandas as pd # Assuming 'df' is your Pandas DataFrame selected_columns = df.iloc[:, df.columns.get_indexer(df.columns[0:3]) + df.columns.get_indexer(df.columns[5:8])] 
  10. Selecting multiple ranges of columns in Pandas DataFrame with list comprehension and iloc[]

    # Description: This query demonstrates selecting multiple ranges of columns in Pandas DataFrame with list comprehension and iloc[]. import pandas as pd # Assuming 'df' is your Pandas DataFrame selected_columns = df.iloc[:, [i for i in range(3)] + [i for i in range(5, 8)]] 

More Tags

connector php-password-hash xmlhttprequest json-serialization objective-c angular-filters kubernetes-jobs django-admin-actions launching-application parameter-passing

More Python Questions

More Stoichiometry Calculators

More General chemistry Calculators

More Genetics Calculators

More Other animals Calculators