简体   繁体   中英

How to insert a column from a DataFrame into a new column of a database table with variable column count using python

I have a DataFrame with data: (Table 1)

column1   column2(variable)
A            2
B            1
C            3
D            5

The DataFrame value ie column2 name and values change every time the scripts are run. So let us consider on next run, values in dataframe are like this:

column1   column2(variable)
A            7
B            8
C            9
D            7

I want to add the new(updated column2 values to database table) so that now the database table becomes something like this:

column1   column2  column3    
A            2        7
B            1        8
C            3        9
D            5        7

I can find many similar questions on stack overflow but non of them seems to solve this, since, here the column count in database table is changing on every run, also in current code scenario, I am unable to create a temporary table and do a transplant as described here: Adding an extra column to (big) SQLite database from Pandas dataframe .

df2 = df.groupby('column1').size().reset_index(name=currentDate)

where currentDate has today's date and hence the table has 2 columns now (column1 and currentdate). I want to add this data to already present Table 1(above) in the database without using a new temporary table(keeping in mind that I have to use SQL query with a variable name ie currentDate).

Do a df.iloc[:,-1].name to get the last column's name that is added in.
You can then add the column via the usual assignment

As per my understanding of your question: Assuming df is your original table and you are trying to add an additional column to it with the same name as the last column in df2:

df[df2.iloc[:,-1].name] = df2.iloc[:,-1]

This means that:

#Original df
column1   column2
A            2
B            1
C            3
D            5
#Original df2
column1   NEW_NAME
A            7
B            8
C            9
D            7
#New df
column1   column2  NEW_NAME    
A            2        7
B            1        8
C            3        9
D            5        7

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