0% found this document useful (0 votes)
3 views

15 commonly asked Python interview questions - Google Docs

The document outlines 15 commonly asked Python interview questions, covering topics such as data types, handling missing data in Pandas, and differences between various functions. Key concepts include the distinction between lists and tuples, the use of groupby, merge, and apply functions in Pandas, as well as methods for managing outliers and filtering DataFrames. Each question is accompanied by examples for clarity.

Uploaded by

bakrabu786
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

15 commonly asked Python interview questions - Google Docs

The document outlines 15 commonly asked Python interview questions, covering topics such as data types, handling missing data in Pandas, and differences between various functions. Key concepts include the distinction between lists and tuples, the use of groupby, merge, and apply functions in Pandas, as well as methods for managing outliers and filtering DataFrames. Each question is accompanied by examples for clarity.

Uploaded by

bakrabu786
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

‭15 commonly asked Python interview questions‬

‭1. Explain the difference between a list and a tuple in Python.‬


‭ ists‬‭are mutable, meaning their elements can be changed,‬‭whereas‬‭tuples‬‭are immutable,‬
L
‭making them more memory-efficient and faster. Lists use square brackets‬‭[]‬‭, while tuples use‬
‭parentheses‬‭()‬‭.‬
‭Example:‬
‭y_list = [1, 2, 3] # Mutable‬
m
my_tuple = (1, 2, 3) # Immutable‬

‭2. How do you handle missing data in a Pandas DataFrame?‬

‭‬
● ‭ se‬‭df.isnull().sum()‬‭to check missing values.‬
U
‭●‬ ‭Fill missing values using‬‭df.fillna(value, method='ffill'/'bfill')‬‭.‬
‭●‬ ‭Remove missing values with‬‭df.dropna()‬‭.‬

‭Example:‬
‭f.fillna(df.mean()) # Fill NaNs with column mean‬
d
df.dropna() # Remove rows with NaNs‬

‭3. What is the purpose of the‬‭groupby()‬‭function in‬‭Pandas?‬


groupby()‬‭is
‭ used to split a DataFrame into groups‬‭based on a column, apply functions, and‬
‭combine results.‬
‭Example:‬
df.groupby('Category')['Sales'].sum()‬

‭4. How can you merge two DataFrames in Pandas?‬


‭Use‬‭pd.merge()‬‭to combine DataFrames on a common column.‬
‭Example:‬
‭erged_df = pd.merge(df1, df2, on='ID', how='inner')
m # 'left', 'right',‬
'outer' also available‬

‭5. Explain the difference between‬‭loc‬‭and‬‭iloc‬‭in‬‭Pandas.‬

‭‬ ‭
● ‭: Label-based indexing.‬
loc[]‬
‭●‬ ‭
iloc[]‬ ‭: Integer-based positional indexing.‬
‭Example:‬
‭f.loc[2, 'Age'] # Access by row label‬
d
df.iloc[2, 1] # Access by row and column index‬

‭6. What are Python's built-in data types?‬

‭‬
● ‭ umeric:‬‭int‬‭,‬‭float‬‭,‬‭complex‬
N
‭●‬ ‭Sequence:‬‭list‬‭,‬‭tuple‬‭,‬‭range‬‭,‬‭str‬
‭●‬ ‭Set:‬‭set‬‭,‬‭frozenset‬
‭●‬ ‭Mapping:‬‭dict‬
‭●‬ ‭Boolean:‬‭bool‬
‭●‬ ‭Binary:‬‭bytes‬‭,‬‭bytearray‬‭,‬‭memoryview‬

‭7. How do you read a CSV file into a Pandas DataFrame?‬


‭Use‬‭pd.read_csv()‬‭.‬
‭Example:‬
df = pd.read_csv('file.csv')‬

‭8. What is the difference between‬‭apply()‬‭and‬‭map()‬‭functions in Pandas?‬

‭‬ ‭
● ‭: Used for Series (element-wise transformations).‬
map()‬
‭●‬ ‭
apply()‬ ‭: Used for both Series and DataFrames (column-wise‬‭or row-wise‬
‭transformations).‬

‭Example:‬
‭f['column'].map(lambda x: x * 2) # Applies function to each value‬
d
df.apply(lambda x: x.sum(), axis=0) # Sum of each column‬

‭9. How do you remove duplicates from a DataFrame?‬


‭Use‬‭df.drop_duplicates()‬‭.‬
‭Example:‬
df.drop_duplicates(subset=['column_name'], keep='first', inplace=True)‬

‭10. Explain the use of lambda functions in Python.‬


‭A lambda function is an anonymous, single-expression function.‬
‭Example:‬
‭quare = lambda x: x**2‬
s
print(square(4)) # Output: 16‬

‭11. What is the difference between‬‭join()‬‭and‬‭merge()‬‭in Pandas?‬

‭‬ ‭
● merge()‬ ‭: Used for complex joins (like SQL joins) on‬‭columns.‬
‭●‬ ‭ ‭: Used for joining on index.‬
join()‬

‭Example:‬
df1.join(df2, on='ID', how='left')‬

‭12. How can you concatenate two DataFrames vertically and horizontally?‬
‭Use‬‭pd.concat()‬‭.‬
‭Example:‬
‭ Vertical (stack rows)‬
#
df_vertical = pd.concat([df1, df2], axis=0)‬

‭ Horizontal (add columns)‬


#
df_horizontal = pd.concat([df1, df2], axis=1)‬

‭13. What is the purpose of the‬‭pivot_table()‬‭function‬‭in Pandas?‬


pivot_table()‬‭summarizes
‭ data by aggregating values‬‭over a specified index and columns.‬
‭Example:‬
df.pivot_table(index='Category', values='Sales', aggfunc='sum')‬

‭14. How do you handle outliers in a dataset using Python?‬

‭‬
● ‭ se‬‭IQR method‬‭: Remove values outside‬‭1.5 * IQR‬‭.‬
U
‭●‬ ‭Use‬‭Z-score‬‭: Remove values with‬‭|Z-score| > 3‬‭.‬
‭●‬ ‭Use‬‭Winsorization‬‭: Cap outliers instead of removing‬‭them.‬

‭Example:‬
‭rom scipy import stats‬
f
df = df[(np.abs(stats.zscore(df['column'])) < 3)]‬

‭15. How do you filter rows in a Pandas DataFrame based on a condition?‬


‭Use boolean indexing.‬
‭Example:‬
filtered_df = df[df['Salary'] > 50000]‬

You might also like