Why am I seeing "TypeError: string indices must be integers"?
The "TypeError: string indices must be integers" error is raised when you try to access an element of a DataFrame or Series using a string index instead of an integer index. This typically happens when you try to access a single column of a DataFrame using the square bracket notation like df['column_name']
instead of the recommended dot notation df.column_name
or the .loc
or .iloc
accessors.
Here's an example of how this error can occur:
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Try to access a single column using string index
# It raises "TypeError: string indices must be integers"
print(df['A'])
The reason behind this error is that when you access a DataFrame using the square bracket notation, it returns a new DataFrame or Series (a new view) containing the data from the column you specified, rather than a single value. And this new view is expecting an integer index to access the value, But you passed a string index instead.
The correct way to access a single column of a DataFrame is to use the dot notation or the .loc
or .iloc
accessors, like this:
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Access a single column using dot notation
print(df.A)
# OR
# Access a single column using the loc accessor
print(df.loc[:, 'A'])
You can also use the .iloc
accessor to access a column by integer position like this:
print(df.iloc[:, 0])
Another common cause of this error is passing a string value instead of an integer for the indexing when trying to access a single cell of a dataframe using the .loc
or .iloc
accessors. For example,
df.loc['a', 'A']
It will raise "TypeError: string indices must be integers" because the first argument of .loc
is expected to be integer index.
It's always a good idea to check the DataFrame or Series you're trying to access and make sure that you're using the correct notation and indexing.