dataframe 与dict 相互转化
import numpy as np
import pandas as pd
map_df=pd.DataFrame({"int":[1,2,3],"celltype":["spk","vis","wal"]})
print(map_df)
print("*"*50)
celltype_map_dict=map_df.set_index('int')['celltype'].to_dict()
print(celltype_map_dict)
映射np.array
arr=np.array([1,2,2,3,1,2,2],dtype=int)
map_arr=np.vectorize(celltype_map_dict.get)(arr)
print(map_arr)
print(type(map_arr))
映射dataframe
pd_df=pd.DataFrame({"label":np.array([1,2,3,2,3,2,1,3,2,1,1,1,2,3])})
print(pd_df)
pd_df["celltype1"]=pd_df["label"].map(celltype_map_dict)
print(pd_df)
pd_df["celltype2"]=pd_df['label'].replace(celltype_map_dict, inplace=False)
print(pd_df)