pandas dataframe 修改列名的方法
加之rename函数,用字典的形式替换式的修改,
df.rename(columns={'a':'A',"b":"B"})
df
三、obj[‘col’] = value 方法
直接对 DataFrame 直接赋值即可
in [6]: data['d'] = 0
in [7]: data
out[7]:
a b c d
0 1 2 3 0
1 4 5 6 0
2 7 8 9 0
————————————————
实例操作:
import pandas as pd import sqlite3 db_file="./cnblogs_blog_weifeng1463.20230912103223.db" conn = sqlite3.connect(db_file, isolation_level=None) db_df = pd.read_sql_query("SELECT Id,Title,DateAdded,Body FROM blog_Content",conn) new_df=db_df.rename(columns={'Id':'id','Title':'title','DateAdded':'date','Body':'body'}) new_df['author_id']= 1 new_df['subtitle']= 1 new_df['img_url']= "http://123.com" print(new_df) #db_df.to_csv('database.csv',encoding='utf_8_sig')
标签:rename,df,列名,db,DataFrame,new,pandas From: https://www.cnblogs.com/weifeng1463/p/17697515.html