Pandas Dataframe.iloc - Sheet1 Flashcards

1
Q

What is pandas.DataFrame.iloc?

A

DataFrame.iloc is a purely integer-location based indexing for selection by position in pandas DataFrame.

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

What are some valid inputs for pandas.DataFrame.iloc?

A

Valid inputs for DataFrame.iloc are:
an integer,
a list or array of integers,
a slice object with integers,
a boolean array,
a callable function with one argument, and a tuple of row and column indexes.

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

How does DataFrame.iloc handle out-of-bounds indexing?

A

DataFrame.iloc will raise an IndexError if a requested indexer is out-of-bounds, except slice indexers which allow out-of-bounds indexing according to python/numpy slice semantics.

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

What happens when you index just the rows with a scalar integer using DataFrame.iloc?

A

When you index just the rows with a scalar integer, it returns a series representing the row.

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

What happens when you index just the rows with a list of integers using DataFrame.iloc?

A

When you index just the rows with a list of integers, it returns a DataFrame representing the selected rows.

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

What happens when you use a slice object with DataFrame.iloc?

A

When you use a slice object, DataFrame.iloc returns a DataFrame that includes rows from the start of the slice to the end.

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

What happens when you use a boolean mask with DataFrame.iloc?

A

When you use a boolean mask with DataFrame.iloc, it returns a DataFrame containing only the rows for which the mask value is True.

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

What happens when you use a callable function with DataFrame.iloc?

A

When you use a callable function, DataFrame.iloc applies the function to the DataFrame and returns the rows for which the function returns True.

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

How do you index both axes using DataFrame.iloc?

A

To index both axes with DataFrame.iloc, you can mix the indexer types for the index and columns. Use : to select the entire axis.

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

What happens when you use scalar integers to index both axes using DataFrame.iloc?

A

When you use scalar integers to index both axes, DataFrame.iloc returns a scalar value which is the value of the selected cell.

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

What happens when you use lists of integers to index both axes using DataFrame.iloc?

A

When you use lists of integers to index both axes, DataFrame.iloc returns a DataFrame with rows and columns that correspond to the selected rows and columns.

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

What happens when you use slice objects to index both axes using DataFrame.iloc?

A

When you use slice objects to index both axes, DataFrame.iloc returns a DataFrame that includes rows and columns from the start of the slice to the end.

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

What happens when you use a boolean array to index both axes using DataFrame.iloc?

A

When you use a boolean array to index both axes, DataFrame.iloc returns a DataFrame with only the columns for which the mask value is True.

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

What happens when you use a callable function to index both axes using DataFrame.iloc?

A

When you use a callable function to index both axes, DataFrame.iloc applies the function to the DataFrame and returns the columns for which the function returns True.

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

How do DataFrame.iloc and DataFrame.loc differ?

A

DataFrame.iloc is primarily integer position based, while DataFrame.loc is label-location based indexer for selection by label.

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

How do DataFrame.iloc and DataFrame.iat differ?

A

DataFrame.iat is a faster but less flexible version of DataFrame.iloc, which is used for getting or setting a single value in a DataFrame or Series.

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

How does DataFrame.iloc handle callable input?

A

DataFrame.iloc handles callable input by passing the DataFrame to the callable function, and the function should return an output that’s valid for indexing.

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

Can you select every other row in a DataFrame using DataFrame.iloc?

A

Yes, you can select every other row in a DataFrame using DataFrame.iloc with a callable function like so: df.iloc[lambda x: x.index % 2 == 0].

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

Can you select the first column in a DataFrame using DataFrame.iloc?

A

Yes, you can select the first column in a DataFrame using DataFrame.iloc like so: df.iloc[:, 0].

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

Can you select the last row in a DataFrame using DataFrame.iloc?

A

Yes, you can select the last row in a DataFrame using DataFrame.iloc like so: df.iloc[-1].

21
Q

Can you select the second and third rows in a DataFrame using DataFrame.iloc?

A

Yes, you can select the second and third rows in a DataFrame using DataFrame.iloc like so: df.iloc[1:3].

22
Q

How can you use DataFrame.iloc to select rows where a certain column’s value is True?

A

To select rows where a certain column’s value is True, you can use DataFrame.iloc with a boolean mask derived from that column.

23
Q

How can you select rows at even indices using DataFrame.iloc?

A

You can select rows at even indices by passing a callable function to DataFrame.iloc, like so: df.iloc[lambda x: x.index % 2 == 0].

24
Q

Can you select rows based on a complex condition using DataFrame.iloc?

A

DataFrame.iloc does not directly support selecting rows based on complex conditions involving the DataFrame’s content, as it’s integer-based. You should use boolean indexing for this instead, possibly in combination with DataFrame.loc.

25
Q

Can you use DataFrame.iloc to set values in a DataFrame?

A

Yes, you can use DataFrame.iloc to set values in a DataFrame by assigning to the selection.

26
Q

How do you change the value of a specific cell using DataFrame.iloc?

A

You can change the value of a specific cell using DataFrame.iloc like so: df.iloc[row_index, column_index] = new_value.

27
Q

How do you change the values of specific rows using DataFrame.iloc?

A

You can change the values of specific rows using DataFrame.iloc like so: df.iloc[row_indices] = new_values.

28
Q

How do you change the values of specific columns using DataFrame.iloc?

A

You can change the values of specific columns using DataFrame.iloc like so: df.iloc[:, column_indices] = new_values.

29
Q

Can DataFrame.iloc be used to add new rows to a DataFrame?

A

DataFrame.iloc is not directly used to add new rows. Use DataFrame.append or pandas.concat to add new rows.

30
Q

Can DataFrame.iloc be used to add new columns to a DataFrame?

A

DataFrame.iloc is not directly used to add new columns. You can add new columns directly with df[‘new_column’] = values.

31
Q

Can you use DataFrame.iloc to delete rows from a DataFrame?

A

DataFrame.iloc itself cannot delete rows, but you can use it to select rows to keep, effectively deleting others. Alternatively, you can use DataFrame.drop with the labels of the rows you want to delete.

32
Q

Can you use DataFrame.iloc to delete columns from a DataFrame?

A

DataFrame.iloc itself cannot delete columns, but you can use it to select columns to keep, effectively deleting others. Alternatively, you can use DataFrame.drop with the labels of the columns you want to delete.

33
Q

What kind of errors can DataFrame.iloc raise?

A

DataFrame.iloc can raise IndexError if a requested indexer is out-of-bounds, and ValueError for invalid inputs.

34
Q

Can DataFrame.iloc handle NaN values?

A

DataFrame.iloc can select rows and columns that have NaN values, but does not have any special handling for them.

35
Q

How do you use DataFrame.iloc to select the first n rows?

A

You can use DataFrame.iloc to select the first n rows like so: df.iloc[:n].

36
Q

How do you use DataFrame.iloc to select the last n rows?

A

You can use DataFrame.iloc to select the last n rows like so: df.iloc[-n:].

37
Q

How do you use DataFrame.iloc to select the first n columns?

A

You can use DataFrame.iloc to select the first n columns like so: df.iloc[:, :n].

38
Q

How do you use DataFrame.iloc to select the last n columns?

A

You can use DataFrame.iloc to select the last n columns like so: df.iloc[:, -n:].

39
Q

What does ‘out-of-bounds’ mean in the context of DataFrame.iloc?

A

Out-of-bounds’ in the context of DataFrame.iloc refers to attempting to access an index that is beyond the size of the DataFrame.

40
Q

How do you use DataFrame.iloc to select a single cell in a DataFrame?

A

You can use DataFrame.iloc to select a single cell by specifying both the row index and column index like so: df.iloc[row_index, column_index].

41
Q

Can DataFrame.iloc be used with a DataFrame with a MultiIndex?

A

Yes, DataFrame.iloc can be used with a DataFrame with a MultiIndex. You need to specify the indices for all levels.

42
Q

What happens when the DataFrame has duplicate indices and you use DataFrame.iloc?

A

DataFrame.iloc treats duplicate indices as separate entities because it uses integer-based position indexing.

43
Q

What does ‘integer position based’ mean in the context of DataFrame.iloc?

A

Integer position based’ in the context of DataFrame.iloc refers to indexing based on the integer positions of the rows/columns, starting from 0.

44
Q

How does DataFrame.iloc behave differently from Python’s standard list indexing?

A

Unlike Python’s standard list indexing, DataFrame.iloc can take a list of integers as input, return DataFrame or Series based on input, and supports callable function input.

45
Q

What is the relationship between DataFrame.iloc and numpy array indexing?

A

DataFrame.iloc indexing semantics align closely with indexing on a numpy array, especially with regards to integer and boolean indexing.

46
Q

Can DataFrame.iloc be used to modify a DataFrame in place?

A

Yes, DataFrame.iloc can be used to modify a DataFrame in place by assigning new values to the selection.

47
Q

Can DataFrame.iloc accept a list of boolean values for indexing?

A

Yes, DataFrame.iloc can accept a list of boolean values for indexing. The list must be the same length as the row or column being indexed.

48
Q

Can DataFrame.iloc accept negative integers for indexing?

A

Yes, DataFrame.iloc can accept negative integers for indexing, similar to standard Python list indexing, where negative integers refer to elements from the end of the list.