简体   繁体   中英

Pandas: Compare dataframe with csv file and update the csv file

I have below ddataframe

          domain   hostname      ip address  status           managed by      monitor target name
0         jetty.com  aabb   XXX.XX.XX.XXX         Deployed  Sec Team  aabb.jetty.com
1         jetty.com  axcd   XXX.XX.XX.XXX         Deployed  Sec Team  axcd.jetty.com

Now I have another csv file reporst.csv

"domain","hostname","ip address","managed by", "monitor target name"
"jetty.com","aabb","XXX.XX.XX.XXX", "Decreased","OP Team", "aabb.jetty.com"
"jetty.com","axcd","XXX.XX.XX.XXX", "Decreased","OP2 Team", "axcd.jetty.com"
"jetty.com","appd","XXX.XX.XX.XXX", "Dec","OP8 Team", "appd.jetty.com"

 

Now I want to compare the df to this csv and then update the csv to the below

"domain","hostname","ip address","managed by", "monitor target name"
"jetty.com","aabb","XXX.XX.XX.XXX", "Deployed","OP Team", "aabb.jetty.com"
"jetty.com","axcd","XXX.XX.XX.XXX", "Deployed","OP Team", "axcd.jetty.com"
"jetty.com","appd","XXX.XX.XX.XXX", "Dec","OP8 Team", "appd.jetty.com"

Now it is updated and look same as it is in the df

How can I update the csv according to the df value but not tough the other rows in the csv which are there already

Use:

df2 = pd.read_csv('reporst.csv')

df1 = ddataframe.set_index(['domain','hostname','ip address'])
df2 = reporst.set_index(['domain','hostname','ip address'])

df2.update(df1)

df2.to_csv('out.csv')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM