简体   繁体   English

通过将列名与字典匹配来填充 dataframe 中的值

[英]Fill values in dataframe by matching column names to dictionary

I need to map the values of a dictionary to the names of the columns of a dataframe.我需要将字典的值 map 转换为 dataframe 的列名。 If there is no value I would like to fill up the dataframe with zeros如果没有值,我想用零填充 dataframe

import pandas as pd
final_df = pd.DataFrame(columns=['Chat','Email','Phone','VM','web','Sum unique values', 'Date', 'Time', 'Month Selected', 'Year Selected'])
export_dict ={'Chat': 1, 'Email': 4, 'VM': 15}

Result:结果:


Chat Email  Phone VM web Sum unique values Date Time Month Selected Year Selected
 1    4      0    15  0         20          0     0    0               0

You can add new row to DataFrame by DataFrame.loc :您可以通过DataFrame.loc向 DataFrame 添加新行:

final_df.loc[0] = export_dict

and then replace misisng values by DataFrame.fillna :然后用DataFrame.fillna替换错误值:

final_df = final_df.fillna(0)
print (final_df)
   Chat  Email  Phone  VM  web  Sum unique values  Date  Time  Month Selected  \
0     1      4      0  15    0                  0     0     0               0   

   Year Selected  
0              0  

Another solutions are with DataFrame costructor for improve performance - here is added all columns by DataFrame.reindex :另一种解决方案是使用 DataFrame 构造函数来提高性能 - 这里是通过DataFrame.reindex添加所有列:

export_dict ={'Chat': 1, 'Email': 4, 'VM': 15}
c =['Chat','Email','Phone','VM','web','Sum unique values',
   'Date', 'Time', 'Month Selected', 'Year Selected']

final_df = pd.DataFrame([export_dict]).reindex(c, axis=1, fill_value=0)
print (final_df)
   Chat  Email  Phone  VM  web  Sum unique values  Date  Time  Month Selected  \
0     1      4      0  15    0                  0     0     0               0   

   Year Selected  
0              0  

Or by merge of dictionaries, order is important of passed value - need first zero dict for avoid overwrite values by 0 :或者通过merge字典,顺序对于传递的值很重要 - 需要第一个零字典以避免用0覆盖值:

final_df  = pd.DataFrame([{**dict.fromkeys(c, 0), **export_dict}])
print (final_df)
   Chat  Email  Phone  VM  web  Sum unique values  Date  Time  Month Selected  \
0     1      4      0  15    0                  0     0     0               0   

   Year Selected  
0              0  

If change order all values are 0 :如果更改顺序所有值都是0

print ( pd.DataFrame([{**export_dict,**dict.fromkeys(c, 0)}]))
   Chat  Email  VM  Phone  web  Sum unique values  Date  Time  Month Selected  \
0     0      0   0      0    0                  0     0     0               0   

   Year Selected  
0              0  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用列名填充 dataframe 的真值? - How to fill true values of a dataframe with column names? 将字典值添加到数据框列名 - Add dictionary values to dataframe column names 如何用另一个列表或字典匹配替换 pandas dataframe 列名 - How to replace pandas dataframe column names with another list or dictionary matching 如何根据条件从数据框列名称填充列值? - How to fill column values from dataframe column names based on condition? 根据与其他列名称匹配的列值填充 Pandas Dataframe - Populate Pandas Dataframe Based on Column Values Matching Other Column Names 如果多列匹配字典中的值,则用一个值填充数据框列 - Fill dataframe column with a value if multiple columns match values in a dictionary 用系列键替换匹配列名中的 DataFrame 值 - Replace DataFrame values within matching column names with Series' keys 如何通过将字典键与列名匹配 python 将 map 字典值与 dataframe 列的值相匹配 - How to map dictionary values to values of dataframe column by matching dictionary key to column name python 字典中的 Append Dataframe 值通过匹配字典键与 Dataframe 列名 ZA7F5F35426B927411FC9231B56 - Append Dataframe values in Dictionary by matching Dictionary key with Dataframe Column name Python 如何通过匹配来自另一个数据帧熊猫的值来填充数据帧中的列的值 - How to fill in values for a column in a dataframe by matching values from another dataframe pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM