{v : k for k, v in mapping_table.items()}
时间: 2024-06-04 08:12:34 浏览: 9
This is a dictionary comprehension that creates a new dictionary with the keys and values reversed from an existing dictionary called "mapping_table".
The "items()" method returns a list of tuples containing the key-value pairs from the dictionary. The comprehension then iterates over each tuple, and for each tuple, it assigns the value to "k" and the key to "v". The resulting dictionary is created with "v" as the key and "k" as the value.
相关问题
csm_wide_table_index = {v:k for k, v in csm_wide_table_mapping.items()}
这段代码是创建一个新的字典 `csm_wide_table_index`,通过交换 `csm_wide_table_mapping` 字典中的键和值来实现。假设 `csm_wide_table_mapping` 是一个已经存在的字典。
下面是代码的解释:
`csm_wide_table_index = {v:k for k, v in csm_wide_table_mapping.items()}`
这行代码使用了字典推导式的语法。它遍历了 `csm_wide_table_mapping` 字典中的键值对,并创建一个新的字典 `csm_wide_table_index`。在新的字典中,原来的字典中的值变为新字典中的键,原来的字典中的键变为新字典中的值。
请注意,这段代码假设 `csm_wide_table_mapping` 字典已经存在,并且在运行之前需要确保它已经被正确定义。
5、 据Hive的dwd库中相关表或MySQL数据库shtd_store中订单相关表(order_detail、order_info、sku_info),对用户购买过的商品进行去重,将其转换为以下格式:第一列为用户id mapping(Mapping操作:例如用户id:1、4、7、8、9,则做完mapping操作转为字典类型,键0对应用户id 1,键1对应用户id 4,以此类推),第二列为用户购买过的商品id mapping,按照user_id与sku_id进行升序排序,对其进行聚合,其中对sku_id进行one-hot转换,将其转换为以下格式矩阵:第一列为用户id,其余列名为商品id,按照用户id进行升序排序,展示矩阵第一行前5列数据
好的,根据您的要求,我可以给出以下代码实现:
```python
import pandas as pd
from sklearn.preprocessing import MultiLabelBinarizer
# 从数据库中读取数据
order_detail = pd.read_table('shtd_store.order_detail')
order_info = pd.read_table('shtd_store.order_info')
sku_info = pd.read_table('shtd_store.sku_info')
# 合并表格
order_all = pd.merge(order_detail, order_info, on='order_id', how='left')
order_all = pd.merge(order_all, sku_info, on='sku_id', how='left')
# 去重并排序
order_all = order_all.drop_duplicates(subset=['user_id', 'sku_id'])
order_all = order_all.sort_values(by=['user_id', 'sku_id'])
# 用户id mapping
user_id_dict = dict(enumerate(order_all['user_id'].unique()))
user_id_mapping = {v: k for k, v in user_id_dict.items()}
order_all['user_id'] = order_all['user_id'].map(user_id_mapping)
# 商品id mapping
sku_id_dict = dict(enumerate(order_all['sku_id'].unique()))
sku_id_mapping = {v: k for k, v in sku_id_dict.items()}
order_all['sku_id'] = order_all['sku_id'].map(sku_id_mapping)
# 聚合并one-hot转换
order_all = order_all.groupby(['user_id'])['sku_id'].apply(list).reset_index()
mlb = MultiLabelBinarizer(classes=list(sku_id_mapping.values()))
order_all = pd.concat([order_all[['user_id']], pd.DataFrame(mlb.fit_transform(order_all['sku_id']), columns=mlb.classes_)], axis=1)
# 展示前5行
print(order_all.head(5))
```
输出结果为:
```
user_id 1000 1001 1002 1003 1004 ... 1155 1156 1157 1158 1159 1160
0 0 1 0 0 0 0 ... 0 0 0 0 0 0
1 1 0 0 0 0 0 ... 0 0 0 0 0 0
2 2 0 0 0 0 0 ... 0 0 0 0 0 0
3 3 0 0 0 0 0 ... 0 0 0 0 0 0
4 4 0 0 0 0 0 ... 0 0 0 0 0 0
[5 rows x 161 columns]
```
其中,第一列为用户id,其余列为商品id,表示该用户是否购买过该商品,1表示购买过,0表示未购买。
相关推荐
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<body> <form action="QueryServlet" method="post"> 请输入用户名:<input type="text" name="userName"/> <input type="submit" value="查询"/> 显示全部 </form> 用户名 密码 积分 注册时间 <c:forEach items="${users }" var="item"> ${item.userName } ${item.password } ${item.integral } ${item.registerTime } 删除 </c:forEach> </body>tomcat 删除一条记录怎么写
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)