import pandas as pd
df = pd.DataFrame([[1,"a"],[2,"c"],[3,"p"]],columns=["user_id","grand"])
df.head()
user_id grand
0 1 a
1 2 c
2 3 p
import redis
redis = redis.Redis(host="localhost", port=6379,password="123",db=0) #不需要认证的不用password,db默认是0
pipe = redis.pipeline()
#此处用hash的方式存入
type="grand"
df.apply(lambda x :pipe.hset(x["user_id"],type,x["grand"]),axis=1)
pipe.execute()
按行操作统计将命令提交到pipline,再用pipe.execute统一执行
redis中存入的无法存入list对象,需要将list转为string存入。