Stack Overflow Asked by afab421 on January 22, 2021
Python
I’m trying to use the below code to make a new dataframe:
new = old[['x', 'y', 'z']]
When I print the old dataframe, it shows me the column value ‘x’ for that column. In other parts of my program, I’m able to call the value ‘x’ to use that column’s information in various things. For reasons I and the internet cannot fathom, I’m getting a KeyError for ‘x’ in this line of code.
(PS PyCharm even tries to be helpful and fill in the ‘x’ value when I begin to type the actual name in this code. It recognizes that it’s there.)
EDIT SOLUTION:
When I used set_index earlier it did something to mess up the ability to read by the x label.
old = pd.read_csv("name.csv")
old['a'] = old['a'].str.replace(",", "")
old['a'] = old['a'].str.replace(" ", "_")
old['x'] = old['x'].str.replace(" ", "_")
old.drop(["b", "c", "d"], axis=1, inplace=True)
old = old.set_index("x")
I think in order to access 'x' you would need to write: new[0][0]
Answered by MrAwesome on January 22, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP