[英]DataFrame accessing nested keys in list within dictionary
The following data is part of a much larger dataframe
with a lot of nested keys. 以下数据是具有大量嵌套键的更大
dataframe
一部分。 Say I want to access "humidity" or "windSpeed" How would I do that? 说我想要访问“湿度”或“windSpeed”我该怎么做?
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. 目的是仅选择某些键并将它们附加到CSV文件,而不是将整个
dataframe
附加到CSV文件。
You'd need to use apply
with a lambda
and index into the dict: 你需要在字典中使用带有
lambda
和index的apply
:
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 我建议不要在df中存储非标量值,因为你失去了使用数据帧的任何矢量化优势,所以它不具备性能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.