1 回答
TA贡献1840条经验 获得超5个赞
import pandas as pd
df2 = pd.DataFrame({'V_ID': ['a','b','c','d'], 'count':[12,5,7,9]})
df = pd.DataFrame({'time':['2008-03-11', '2008-03-11', '2008-03-11','2008-03-11', '2008-03-11', '2008-03-11', '2008-03-11'],
'V_ID': ['a', 'sdf', 'c','rge', 'gfg', 'a', 'a']})
# Create an index column for df2
df2 = df2.reset_index()
# Key-value pairs of index and V_ID
mapping = df2['V_ID'].to_dict()
# Invert key-value pairs
mapping = {v: k for k, v in mapping.items()}
# Replace values in df['V_ID'] that matches with keys in mapping with values
df['V_ID'] = df['V_ID'].replace(mapping)
print(df)
time V_ID
0 2008-03-11 0
1 2008-03-11 sdf
2 2008-03-11 2
3 2008-03-11 rge
4 2008-03-11 gfg
5 2008-03-11 0
6 2008-03-11 0
添加回答
举报