Q1: What is pandas in Python?
Answer : Pandas is a powerful open-source data manipulation and analysis library for Python. It provides data structures and functions for efficiently handling and processing structured data, such as tables or time series data.
Q2: How can you import the pandas library in Python?
Answer : To import the pandas library, you can use the following statement:
import pandas as pd
Q3: What are the primary data structures provided by pandas?
Answer : The primary data structures in pandas are the DataFrame and Series. A DataFrame is a 2-dimensional labeled data structure with columns of potentially different data types, similar to a table in a relational database. A Series is a 1-dimensional labeled array capable of holding any data type.
Q4: How can you create a DataFrame from a dictionary in pandas?
Answer : You can create a DataFrame from a dictionary using the pd.DataFrame() function. Each key-value pair in the dictionary represents a column, where the key is the column name and the value is a list or array containing the column's data. Here's an example:
data = {'Name': ['John', 'Emma', 'Alex'],
'Age': [25, 30, 28],
'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
Q5: How can you select a specific column from a DataFrame in pandas?
Answer You can select a specific column from a DataFrame using the column name as an index. For example, to select the 'Name' column from the 'df' DataFrame created in the previous question, you can use:
name_column = df['Name']
Q6: How can you filter rows in a DataFrame based on a condition in pandas?
Answer : You can filter rows in a DataFrame based on a condition by using boolean indexing. For example, to filter rows where the age is greater than 25 in the 'df' DataFrame, you can use:
filtered_df = df[df['Age'] > 25]
Q7: How can you drop missing values from a DataFrame in pandas?
Answer : You can drop missing values from a DataFrame using the dropna() function. By default, it drops any row that contains at least one missing value. Here's an example:
clean_df = df.dropna()
Q8: How can you apply a function to each element in a DataFrame column in pandas?
Answer : You can apply a function to each element in a DataFrame column using the apply() function. It takes a function as an argument and applies it to each element in the column. For example, to convert the 'Age' column in the 'df' DataFrame to a string, you can use:
df['Age'] = df['Age'].apply(str)
Q9: How can you merge two DataFrames in pandas?
Answer : You can merge two DataFrames in pandas using the merge() function. It combines rows from two DataFrames based on a common column or index. Here's an example of merging two DataFrames based on a common 'ID' column:
merged_df = pd.merge(df1, df2, on='ID')
Q10: How can you save a DataFrame to a CSV file in pandas?
Answer : You can save a DataFrame to a CSV file using the to_csv() function. It takes the file path as an argument and writes the DataFrame's contents to a CSV file. Here's an example:
df.to_csv('data.csv', index=False)
Comentários