Reputation: 65
The following data is part of a much larger dataframe
with a lot of nested keys.
Say I want to access "humidity" or "windSpeed" How would I do that?
df = pd.DataFrame({"data":[{"time":1422802800,"humidity":0.62,"windSpeed":2.62}]})
The purpose is to select only certain keys and append them to a CSV file, rather then appending the entire dataframe
to the CSV file.
Upvotes: 1
Views: 619
Reputation: 393963
You'd need to use apply
with a lambda
and index into the dict:
In[69]:
df['data'].apply(lambda x: x['time'])
Out[69]:
0 1422802800
Name: data, dtype: int64
and like-wise for humidity:
In[71]:
df['data'].apply(lambda x: x['humidity'])
Out[71]:
0 0.62
Name: data, dtype: float64
I'd advise against storing non-scalar values in a df, it's non-performant as you lose any vectorised advantages of using dataframes
Upvotes: 3