Untitled Deck Flashcards
(50 cards)
What is the syntax to read a CSV file in pandas?
pd.read_csv(‘filename.csv’)
How do you read an Excel file with pandas?
pd.read_excel(‘filename.xlsx’, sheet_name=’Sheet1’)
What’s the syntax to save a DataFrame to a CSV file?
df.to_csv(‘filename.csv’, index=False)
How do you read a JSON file in pandas?
pd.read_json(‘filename.json’)
What parameter sets the column delimiter when reading a CSV?
sep=’,’ or delimiter=’,’
How do you create a DataFrame from a dictionary?
pd.DataFrame({‘col1’: [1, 2], ‘col2’: [3, 4]})
What’s the syntax to create a Series?
pd.Series([1, 2, 3, 4])
How do you create a DatetimeIndex?
pd.date_range(start=’2023-01-01’, periods=10, freq=’D’)
How do you create a DataFrame with specific index values?
pd.DataFrame(data, index=[‘a’, ‘b’, ‘c’])
What’s the syntax to create a MultiIndex DataFrame?
pd.DataFrame(data, index=pd.MultiIndex.from_tuples([(‘a’, 1), (‘a’, 2), (‘b’, 1)]))
How do you select a column from a DataFrame?
df[‘column_name’] or df.column_name
What’s the difference between loc and iloc?
loc uses labels for indexing, iloc uses integer positions
How do you select rows 5 through 10 with iloc?
df.iloc[5:11]
How do you select rows where column ‘A’ > 5?
df[df[‘A’] > 5] or df.loc[df[‘A’] > 5]
How do you select the first 5 rows of a DataFrame?
df.head(5) or df.iloc[:5]
How do you drop rows with missing values?
df.dropna()
How do you fill missing values with a specific value?
df.fillna(value)
How do you drop duplicate rows?
df.drop_duplicates()
How do you replace all instances of ‘old_value’ with ‘new_value’?
df.replace(‘old_value’, ‘new_value’)
How do you check for missing values in a DataFrame?
df.isna() or df.isnull()
What’s the syntax for applying a function to each element in a DataFrame?
df.applymap(func)
How do you apply a function to each column in a DataFrame?
df.apply(func)
How do you apply a function to each element in a Series?
series.map(func)
How do you rename columns in a DataFrame?
df.rename(columns={‘old_name’: ‘new_name’})