[英]How to Append a Column to a Dataframe which is inside a Dictionary
`dict = {k:v for k,v in df.groupby(['Year','Month','Day'])} this is my first dictionary. `dict = {k:v for k,v in df.groupby(['Year','Month','Day'])} 这是我的第一本字典。 dataframe_sample dict I'm working on a data task. dataframe_sample dict我正在处理数据任务。 I'm producing some anomaly scores and I'm assigning these scores to empty dictionaries.我正在产生一些异常分数,并将这些分数分配给空字典。 Also I have more than one dataframe in another dictionary.另外我在另一本字典中有不止一个 dataframe 。 I want to append the new column that I've produced as a result of anomaly detection and I want to append this column to each dataframe in my dictionary.我想 append 作为异常检测的结果生成的新列,我想 append 将此列添加到我的字典中的每个 dataframe。 Is there any way to do this?有没有办法做到这一点? Thanks.谢谢。 belows are mine empty dictionaries以下是我的空字典
result_anomaly_clustering={}
result_hbos_scoring={}
result_mad_based_outlier={}
for key in list(df_dict):
result_anomaly_clustering[key]=anomaly_clustering(2,df_dict[key])
result_hbos_scoring[key]=hbos_scoring (0.05,5,df_dict[key])
result_mad_based_outlier[key]=mad_based_outlier(3.5,df_dict[key])
Above code is my anomaly algorithm functions that I'm calling上面的代码是我正在调用的异常算法函数
I found that the main problem is because of indexing in the functions.我发现主要问题是因为函数中的索引。 When I fixed the indexing in the functions, the problem was solved.当我修复函数中的索引时,问题就解决了。 Thanks for your help.谢谢你的帮助。
# sample dictionary with anomaly scores
anomalies = {1: 2, 2:8}
# sample dictionary with dataframes
data_frames = {1: pd.DataFrame(data={'a': [4,4]}), 2:pd.DataFrame(data={'b':[6,6,7]})}
# iterate over key, value pairs of the dictionary of dataframes
# and create column score taken from anomalies dict by key
# with key matching anomalies dict key.
for k,v in data_frames.items():
v['score'] = anomalies[k]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.