'KeyError string in the dataframe

enter image description here us_permit["Population estimates, July 1, 2016, (V2016)"] I have a dataframe that has a very long string column, but whenever I select those columns, it returns an error

KeyError: 'Population estimates, July 1, 2016, (V2016)'

why is that?



Solution 1:[1]

Use:

census_2016_df = census_df[[0,1]]

This will give you state and Population estimates, July 1, 2016, (V2016) on your table.

Solution 2:[2]

df.columns or df.keys() return array of columns that on dataframe. you can print or just call using the index. for example df[df.columns[0]] will return the first column of the dataframe

or if you want to get the first and second columns as dataframe, you can use df[[df.columns[0],df.columns[1]]]. that will return dataframe with first and second column

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tyler2P
Solution 2 dimas krisrianto