在具有整数的Pandas Dataframe中映射字符串值 [英] Map string values in a Pandas Dataframe with integers

查看:270
本文介绍了在具有整数的Pandas Dataframe中映射字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在熊猫中 DataFrame 如何在一列中用整数映射字符串。我在 DataFrame 中有大约500个字符串,需要用'1'开头的整数替换它们。

In Pandas DataFrame how to map strings in one column with integers. I have around 500 strings in the DataFrame and need to replace them with integers starting with '1'.

样本 DataFrame

                                    Request  count
547             GET /online/WebResource.axd  37506
424              GET /online/2/2/22001.aspx  13315
699          POST /online/2/6/1/261001.aspx  13236
546          GET /online/ScriptResource.axd  12255
492               GET /online/2/6/Home.aspx  10462
660             POST /online/2/2/22001.aspx   9803

我把所有的请求都列入了一个列表。

I have taken all the Requests in to a list.

requestlist = df.Request.unique()

不知道如何使用1-500映射这些请求。类似的问题 python pandas用数字替换datarame中的字符串

No idea of how to map these Requests with 1-500. Similar question. python pandas replacing strings in datarame with numbers

推荐答案

所以你可以做的是构建一个临时数据框,并将它合并回现有的数据框:

So what you could do is construct a temporary dataframe and merge this back to your existing dataframe:

temp_df = pd.DataFrame({'Request': df.Request.unique(), 'Request_id':range(len(df.Request.unique()))})

现在将其合并回原始数据框

Now merge this back to your original dataframe

df = df.merge(temp_df, on='Request', how='left')

这篇关于在具有整数的Pandas Dataframe中映射字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆