在具有整数的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')