简体   繁体   English

如何将 Append 列到字典中的 Dataframe

[英]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.

相关问题 将数据框中的列追加到字典python - append column in dataframe to dictionary python 如何将 append Python 字典到 Pandas ZBA834BA059A9A379459C112175EB88E 匹配到列名键 - How to append Python dictionary to Pandas DataFrame, matching the key to the column name 如何将字典中每个数据帧的列附加到另一个字典中的另一个数据帧? - How to append a column from each dataframe in a dictionary, to another dataframe in another dictionary? 如何从 dataframe 列中的字典和原始 dataframe 中提取值 - How to extract value from a dictionary inside a column of dataframe and to the orginal dataframe 如何将字典附加到pandas数据帧? - How to append a dictionary to a pandas dataframe? 如何根据列值在数据框单元格内附加字符串 - How to append strings inside dataframe cells based on column values 我如何获得数据帧的一列的值是一个字典 - How can I get the values of a column of a dataframe which are a dictionary 如何访问字典列表中的值,该列表是 oneliner 中 dataframe 中的一列 - How to access values from lists of dictionary which is a column in dataframe in oneliner 如何将 dataframe 列内的嵌套字典拆分为新行? - how to split a nested dictionary inside a column of a dataframe into new rows? 如何在 dataframe 列中的 append 值 - how to append value in a dataframe column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM