[DATAFRAME ] Class XII access rows and columns using different methods

To work with columns and rows in a DataFrame using Python, you'll typically use the pandas library, which provides powerful data manipulation tools. Below is a basic example to demonstrate how to create a DataFrame, access its columns and rows, and perform some common operations. How to access columns and rows in DataFrame.


Key Points:

  1. Accessing Columns:

    • You can access a single column using df['ColumnName'].
    • Multiple columns can be accessed using df[['Column1', 'Column2']].
  2. Accessing Rows:

    • Use df.iloc[index] for accessing rows by their integer location.
    • Use df.loc[index] for accessing rows by their label/index.
  3. Accessing Specific Elements:

    • df.at[row_index, 'ColumnName'] accesses a specific element based on row and column labels.
  4. Filtering Rows:

    • You can filter rows based on a condition, such as df[df['Age'] > 25] to get all rows where the 'Age' column has values greater than 25.

# Accessing column Single Rows and multiple rows

import pandas as pd # Sample data data = { 'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [24, 27, 22, 32], 'City': ['New York', 'Los Angeles', 'Chicago', 'Houston'] } # Create DataFrame df = pd.DataFrame(data) # Display the DataFrame print("DataFrame:") print(df) # Accessing columns print("\nAccessing the 'Name' column:") print(df['Name']) # Accessing multiple columns print("\nAccessing the 'Name' and 'City' columns:") print(df[['Name', 'City']])




# Accessing column Single and multiple columns

import pandas as pd # Sample data data = { 'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [24, 27, 22, 32], 'City': ['New York', 'Los Angeles', 'Chicago', 'Houston'] } # Create DataFrame df = pd.DataFrame(data) # Display the DataFrame print("DataFrame:") print(df) print(" Accessing columns ") print(df.Name)











# Accessing row using Default index Single Rows and
multiple rows
import pandas as pd # Sample data data = { 'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [24, 27, 22, 32], 'City': ['New York', 'Los Angeles', 'Chicago', 'Houston'] } # Create DataFrame df = pd.DataFrame(data) # Accessing rows using iloc (integer-location based indexing) print("\nAccessing the first row:") print(df.iloc[0]) print("\nAccessing the Multiple rows:") print(df.iloc[0:3])




# Accessing row using given index Single Rows and
multiple rows

import pandas as pd

# Sample data
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David'],
    'Age': [24, 27, 22, 32],
    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']
}

# Create DataFrame
df = pd.DataFrame(data)
print(df)

# Accessing rows using loc (label based indexing)
print("\nAccessing the row where index is 1:")
print(df.loc[1])

print("\nAccessing the rows where index is 1:3")
print(df.loc[1:3])





# Accessing row and column or specific values

import pandas as pd
# Sample data
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David'],
    'Age': [24, 27, 22, 32],
    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']
}

# Create DataFrame
df = pd.DataFrame(data)
print(df)

# Accessing a specific element (row and column)
print("\nAccessing the element in the second row and 'City' column:")
print(df.at[1, 'City'])







# Accessing row and column or specific values

import pandas as pd
# Sample data
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David'],
    'Age': [24, 27, 22, 32],
    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']
}

# Create DataFrame
df = pd.DataFrame(data)
print(df)

# Accessing a specific element (row and column)
print("\nAccessing the element in the second row and 0 column:")
print(df.iat[1, 0])
print("\nAccessing the element in the 3 row and 2 column:")
print(df.iat[2, 2])


Post a Comment

0 Comments