update csv file in python using pandas

Solutions on MaxInterview for update csv file in python using pandas by the best coders in the world

showing results for - "update csv file in python using pandas"
Carmela
02 Jan 2017
1I was able to get the desired data frame.
2
3import pandas as pd
4import numpy as np
5
6df1 = pd.read_csv('\\dir\\test1.csv', index_col=0)
7df2 = pd.read_csv('\\dir\\test2.csv', index_col=0)
8
9new_index = list(set(list(df1.index.values)+list(df2.index.values)))
10df2 = df2.reindex(new_index)
11df2 = df2.join(df1, rsuffix='_P')
12df2 = df2.loc[:,~df2.columns.str.endswith('_P')].fillna(df1).fillna(0)
13df2.sort_index(inplace=True)
14print df2.to_string()
15
16
17       col2  col3  col4  col1                        
18test1     8     7    15    11
19test3     5     9    10     9
20test5     9    -1     0    12
21test7     1     4     9     0
22test9    11    10    12     0