Untitled Deck Flashcards

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
Q

How do you convert a column’s data type?

A

df[‘column’] = df[‘column’].astype(‘int64’)

26
Q

What’s the basic syntax for a GroupBy operation?

A

df.groupby(‘column’).agg({‘target_column’: ‘mean’})

27
Q

How do you calculate column means in a DataFrame?

A

df.mean() or df.mean(axis=0)

28
Q

How do you calculate row sums in a DataFrame?

A

df.sum(axis=1)

29
Q

How do you get descriptive statistics for a DataFrame?

A

df.describe()

30
Q

How do you create a pivot table in pandas?

A

pd.pivot_table(df, values=’D’, index=[‘A’, ‘B’], columns=[‘C’])

31
Q

How do you concatenate two DataFrames vertically?

A

pd.concat([df1, df2], axis=0)

32
Q

How do you merge two DataFrames on a common column?

A

pd.merge(df1, df2, on=’common_column’)

33
Q

What’s the syntax for a left join in pandas?

A

pd.merge(df1, df2, on=’key’, how=’left’)

34
Q

How do you join DataFrames using their indices?

A

pd.merge(df1, df2, left_index=True, right_index=True)

35
Q

How do you concatenate DataFrames horizontally?

A

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

36
Q

How do you resample a time series to monthly frequency?

A

df.resample(‘M’).mean()

37
Q

How do you create a DatetimeIndex from a string column?

A

df[‘date’] = pd.to_datetime(df[‘date_str’])

38
Q

How do you set a datetime column as index?

A

df.set_index(‘date_column’, inplace=True)

39
Q

How do you get the year from a datetime column?

A

df[‘date’].dt.year

40
Q

How do you calculate the difference between two dates?

A

(df[‘end_date’] - df[‘start_date’]).dt.days

41
Q

How do you perform a rolling window calculation?

A

df.rolling(window=3).mean()

42
Q

What’s the syntax for creating a crosstab?

A

pd.crosstab(df[‘A’], df[‘B’])

43
Q

How do you reshape data from wide to long format?

A

pd.melt(df, id_vars=[‘A’], value_vars=[‘B’, ‘C’])

44
Q

How do you create dummies (one-hot encoding) from a categorical column?

A

pd.get_dummies(df[‘category_column’])

45
Q

How do you calculate correlation between columns?

46
Q

How do you display all columns of a DataFrame?

A

pd.set_option(‘display.max_columns’, None)

47
Q

What method shows basic information about a DataFrame?

48
Q

How do you check the memory usage of a DataFrame?

A

df.memory_usage(deep=True)

49
Q

How do you reset a DataFrame’s index?

A

df.reset_index()

50
Q

How do you save a DataFrame to an HDF5 store?

A

df.to_hdf(‘store.h5’, key=’df’)