Python Editor

DataFrames using Pandas



# Import the pandas library import pandas as pd # Create a sample DataFrame data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, 30, 35, 28], 'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']} df = pd.DataFrame(data) # Display the DataFrame print("Original DataFrame:") print(df) # Accessing specific columns print("\nAccessing 'Name' column:") print(df['Name']) # Accessing specific rows print("\nAccessing the first row:") print(df.iloc[0]) # Filtering data based on conditions print("\nPeople above the age of 30:") print(df[df['Age'] > 30]) # Adding a new column df['Salary'] = [50000, 60000, 75000, 48000] print("\nDataFrame with the 'Salary' column:") print(df) # Deleting a column df = df.drop(columns=['City']) print("\nDataFrame after deleting the 'City' column:") print(df)

Program Explanation -

No comments:

Post a Comment