简体   繁体   English

从其他数据帧中分配一个数字 - Series([], Name: id, dtype: float64)

[英]Attribuate a number from an other Dataframe - Series([], Name: id, dtype: float64)

I have two pandas Dataframes, the first one named source in which we have ID and Names (ID, Username) source.head()我有两个 Pandas Dataframes,第一个名为 source,其中我们有 ID 和 Names (ID, Username) source.head()

在此处输入图片说明

The second one named data_code, in which we have also just unsernames (0) column and an code column in which I will try to get IDs in it.第二个名为 data_code,其中我们也只有 unsernames (0) 列和一个代码列,我将尝试在其中获取 ID。

data_code.head()

在此处输入图片说明

What I did is to create a function that will look for sames Usenames in two Dataframes and get the ID of the username from source Dataframe, if does not exist it will generate a random ID.我所做的是创建一个函数,该函数将在两个 Dataframe 中查找相同的 Usenames,并从源 Dataframe 中获取用户名的 ID,如果不存在,它将生成一个随机 ID。 In my solution I tried to create a dictionary in which I will have only unique values.在我的解决方案中,我尝试创建一个字典,其中只有唯一值。

uniqueIDs = data_code[0].unique()
FofToID= {}

Then I will fill the dictionary with Id using this function然后我将使用这个函数用 Id 填充字典

for i in range(len(uniqueIDs)):   
  if uniqueIDs[i] in list(source["username"]): 
    FofToID[uniqueIDs[i]]= np.float_(source[source["username"]==i]["id"])  
  else:
    FofToID[uniqueIDs[i]]= int(random.random()*10000000)

the output was like below:输出如下: 在此处输入图片说明 My problem that all values existing in the source Dataframe get the value Series([], Name: id, dtype: float64).我的问题是source帧中存在的所有值都获得值 Series([], Name: id, dtype: float64)。 I tried to fix this problem but I failed.我试图解决这个问题,但我失败了。

Use merge with left join, for non exist values id use fillna and last create Series by set_index with to_dict :使用左连接merge ,对于不存在的值id使用fillna并最后通过set_indexto_dict创建Series

source = pd.DataFrame({'id':[111111,222222,666666,888888], 'username':['aa','ss','dd','ff']})
data_code = pd.DataFrame({'code':[0]*4, 0:['ss','dd','rr','yy']})


FofToID = (data_code.merge(source, left_on=0, right_on='username', how='left')
               .fillna({'id': int(random.random()*10000000)})
               .set_index(0)['id']
               .to_dict()
                )
print (FofToID)
{'ss': 222222.0, 'dd': 666666.0, 'rr': 367044.0, 'yy': 367044.0}

I would like to thanks @jezrael for his contribution, here is the final solution I get it:我要感谢@jezrael 的贡献,这是我得到的最终解决方案:

for i in range(len(uniqueIDs)):
if uniqueIDs[i] in list(source["username"]): FofToID[uniqueIDs[i]]= int(source[source["username"]==uniqueIDs[i]]["id"])
else: FofToID[uniqueIDs[i]]= int(random.random()*10000000)

the output look like that输出看起来像这样

在此处输入图片说明

暂无
暂无

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

相关问题 使用 dtype 将 dataframe 从 float64 转换为 object - converting a dataframe from float64 to object using dtype DataFrame之和的奇怪output。 为什么 df.sum() 返回 Series([], dtype: float64)? - Strange output of sum of DataFrame. Why does df.sum() return Series([], dtype: float64)? 使用dtype float64创建pandas数据框会更改其条目的最后一位数(相当大的数字) - creating pandas dataframe with dtype float64 changes last digit of its entry (a fairly large number) 有没有办法防止dtype在重新索引/上采样时间序列时从Int64更改为float64? - Is there a way to prevent dtype from changing from Int64 to float64 when reindexing/upsampling a time-series? 数字是 float64 吗? - Is a number float64? 将 dtype(O) 和“float64”数组合并到 dataframe 时,项目数错误且传递值的形状不匹配 - Wrong number of items and non match shape of passed values when to merge dtype(O) and 'float64' array to a dataframe 弃用警告:在未来版本警告中,空系列的默认 dtype 将是“object”而不是“float64” - DeprecationWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version warning 无法从 dtype(&#39; - Cannot cast array data from dtype('<M8[ns]') to dtype('float64') according to the rule 'safe' 类型错误:无法根据规则“安全”将数组数据从 dtype(&#39;O&#39;) 转换为 dtype(&#39;float64&#39;) - TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' 无法将数组数据从dtype(&#39;O&#39;)转换为dtype(&#39;float64&#39;) - Cannot cast array data from dtype('O') to dtype('float64')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM