mysql - how to group by multi-index(including initial number index and other columns) in python dataframe? -
i working on groupby in python's pd.dataframe. task in code want group data because want make sure no matter how many times query , output data mysql, won't mess raw data.
df1=pd.dataframe(df) #this dataframe multiple different lines of 'open' 1 'symbol' df2=pd.read_sql('select * 6openposition',con=conn) df2=df2.append(df1) df2=df2.groupby(['symbol']).agg({'open':'first'}) df2.to_sql(name='6openposition', con=conn, if_exists='replace', index= false, flavor = 'mysql') #example raw data: symbol open 0 10 1 aa 20 2 aa 30 3 aaa 40 4 aaa 50 5 aaa 50 #after query data multiple times(i appended): symbol open 0 10 1 aa 20 2 aa 30 3 aaa 40 4 aaa 50 5 aaa 50 0 aa 30 1 aaa 40 2 aaa 50 3 aaa 50 4 aaa 60 #how code ended with: symbol open 0 10 1 aa 20 2 aaa 40 #what want: symbol open 0 10 1 aa 20 2 aa 30 3 aaa 40 4 aaa 50 5 aaa 50 6 aaa 60 my raw data have multiple value in column 'open' same 'symbol'. eliminate influence of multiple times of input mysql, raw data here influenced.
my thought on solving problem group initial index , 'symbol' @ same time because after append initial indices 'group by' column. initial indices [0,1,2,...]. if 'symbol' , initial indices same, take first value of 'open' in group. group initial indices could:
df2=df2.groupby(level=0).agg({'open':'first'}) #this code combine lines same indices , take first value of 'open' column but have no idea how combine 'level=0' 'level='symbol''. teach me how group 2 columns including initial indices , column? or tell me way eliminate multiple times of input not messing raw data.
starting df, including index seems indicate whether data repeated:
symbol open 0 10 1 aa 20 2 aa 30 3 aaa 40 4 aaa 50 5 aaa 50 2 aa 30 3 aaa 40 4 aaa 50 5 aaa 50 use
df.reset_index().drop_duplicates().drop('index', axis=1) (keeps first occurrence by default) get:
symbol open 0 10 1 aa 20 2 aa 30 3 aaa 40 4 aaa 50 5 aaa 50
Comments
Post a Comment