Dataframe Keyerror, Although It Exists
Given the data rows = [ {'x': 1, 'y': 2, 'z': 3}, {'x': 2, 'y': 2, 'z': 3}, ] if I try constructing a dataframe like this frame = pd.DataFrame.from_records(rows, index='x'
Solution 1:
Add x to your columns:
df = pd.DataFrame.from_records(rows, index='x', columns=['x', 'y', 'z'])
Solution 2:
You need to include x in you columns. Eg:
rows = [{'x': 1, 'y': 2, 'z': 3}, {'x': 2, 'y': 2, 'z': 3}]
frame = pd.DataFrame.from_records(rows, index='x')
display(frame)
y z
x123223
frame = pd.DataFrame.from_records(rows, index='x', columns=['x', 'y', 'z'])
display(frame)
y z
x123223Solution 3:
Use:
import pandas as pd
rows = [
{'x': 1, 'y': 2, 'z': 3},
{'x': 2, 'y': 2, 'z': 3},
]
frame = pd.DataFrame.from_records(rows, index='x', columns=['x', 'y', 'z'])
print(frame)
Output:
y z
x
1 2 3
2 2 3
Solution 4:
columns is applied as a filter before your index argument is processed. Therefore, you see KeyError as Pandas cannot find x after filtering for ['y', 'z']. One solution is to reindex after reading data into a dataframe:
frame = pd.DataFrame.from_records(rows, index='x').reindex(columns=['y', 'z'])
Alternatively, you can specify all fields, including your index:
frame = pd.DataFrame.from_records(rows, index='x', columns=['x', 'y', 'z'])
This has the benefit of not reading in unwanted fields and filtering as a subsequent step.
Post a Comment for "Dataframe Keyerror, Although It Exists"