Untitled Deck Flashcards

(50 cards)

1
Q

What is the syntax to read a CSV file in pandas?

A

pd.read_csv(‘filename.csv’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you read an Excel file with pandas?

A

pd.read_excel(‘filename.xlsx’, sheet_name=’Sheet1’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the syntax to save a DataFrame to a CSV file?

A

df.to_csv(‘filename.csv’, index=False)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you read a JSON file in pandas?

A

pd.read_json(‘filename.json’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What parameter sets the column delimiter when reading a CSV?

A

sep=’,’ or delimiter=’,’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you create a DataFrame from a dictionary?

A

pd.DataFrame({‘col1’: [1, 2], ‘col2’: [3, 4]})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s the syntax to create a Series?

A

pd.Series([1, 2, 3, 4])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you create a DatetimeIndex?

A

pd.date_range(start=’2023-01-01’, periods=10, freq=’D’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you create a DataFrame with specific index values?

A

pd.DataFrame(data, index=[‘a’, ‘b’, ‘c’])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What’s the syntax to create a MultiIndex DataFrame?

A

pd.DataFrame(data, index=pd.MultiIndex.from_tuples([(‘a’, 1), (‘a’, 2), (‘b’, 1)]))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you select a column from a DataFrame?

A

df[‘column_name’] or df.column_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What’s the difference between loc and iloc?

A

loc uses labels for indexing, iloc uses integer positions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you select rows 5 through 10 with iloc?

A

df.iloc[5:11]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you select rows where column ‘A’ > 5?

A

df[df[‘A’] > 5] or df.loc[df[‘A’] > 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you select the first 5 rows of a DataFrame?

A

df.head(5) or df.iloc[:5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you drop rows with missing values?

A

df.dropna()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you fill missing values with a specific value?

A

df.fillna(value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How do you drop duplicate rows?

A

df.drop_duplicates()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you replace all instances of ‘old_value’ with ‘new_value’?

A

df.replace(‘old_value’, ‘new_value’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How do you check for missing values in a DataFrame?

A

df.isna() or df.isnull()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What’s the syntax for applying a function to each element in a DataFrame?

A

df.applymap(func)

22
Q

How do you apply a function to each column in a DataFrame?

A

df.apply(func)

23
Q

How do you apply a function to each element in a Series?

A

series.map(func)

24
Q

How do you rename columns in a DataFrame?

A

df.rename(columns={‘old_name’: ‘new_name’})

25
How do you convert a column's data type?
df['column'] = df['column'].astype('int64')
26
What's the basic syntax for a GroupBy operation?
df.groupby('column').agg({'target_column': 'mean'})
27
How do you calculate column means in a DataFrame?
df.mean() or df.mean(axis=0)
28
How do you calculate row sums in a DataFrame?
df.sum(axis=1)
29
How do you get descriptive statistics for a DataFrame?
df.describe()
30
How do you create a pivot table in pandas?
pd.pivot_table(df, values='D', index=['A', 'B'], columns=['C'])
31
How do you concatenate two DataFrames vertically?
pd.concat([df1, df2], axis=0)
32
How do you merge two DataFrames on a common column?
pd.merge(df1, df2, on='common_column')
33
What's the syntax for a left join in pandas?
pd.merge(df1, df2, on='key', how='left')
34
How do you join DataFrames using their indices?
pd.merge(df1, df2, left_index=True, right_index=True)
35
How do you concatenate DataFrames horizontally?
pd.concat([df1, df2], axis=1)
36
How do you resample a time series to monthly frequency?
df.resample('M').mean()
37
How do you create a DatetimeIndex from a string column?
df['date'] = pd.to_datetime(df['date_str'])
38
How do you set a datetime column as index?
df.set_index('date_column', inplace=True)
39
How do you get the year from a datetime column?
df['date'].dt.year
40
How do you calculate the difference between two dates?
(df['end_date'] - df['start_date']).dt.days
41
How do you perform a rolling window calculation?
df.rolling(window=3).mean()
42
What's the syntax for creating a crosstab?
pd.crosstab(df['A'], df['B'])
43
How do you reshape data from wide to long format?
pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])
44
How do you create dummies (one-hot encoding) from a categorical column?
pd.get_dummies(df['category_column'])
45
How do you calculate correlation between columns?
df.corr()
46
How do you display all columns of a DataFrame?
pd.set_option('display.max_columns', None)
47
What method shows basic information about a DataFrame?
df.info()
48
How do you check the memory usage of a DataFrame?
df.memory_usage(deep=True)
49
How do you reset a DataFrame's index?
df.reset_index()
50
How do you save a DataFrame to an HDF5 store?
df.to_hdf('store.h5', key='df')