Update multiple rows in mysql table with single call - python

I have a table in MySql DB. I need to update a particular column for all the rows in the table.
I need to do this in a single call without deleting the rows, only updating the column values.
I have tried using df.to_sql(if_exists='replace') but this deletes the rows and re-inserts them. Doing so drops the rows from other table which are linked by the foreign key.
merge_df = merge_df[['id', 'deal_name', 'fund', 'deal_value']]
for index, row in merge_df.iterrows():
ma_deal_obj = MA_Deals.objects.get(id=row['id'])
ma_deal_obj.deal_value = row['deal_value']
ma_deal_obj.save()
merge_df has other columns as well. I only need to update the 'deal_value' column for all rows.
One solution I have is by iterating over the dataframe rows and using Django ORM to save the value but this is quite slow for too many rows.

Related

Add dataframe column WITH VARYING VALUES to MySQL table?

Pretty simple question, but not sure if it’s possible from what I’ve seen so far online.
To keep it simple, let’s say I have a MySQL table with 1 column and 5 rows made already. If I have a pandas dataframe with 1 column and 5 rows, how can I add that dataframe column (with its values) to the database table?
The guides I’ve read so far only show you how to simply create a new column with either null values or 1 constant value, which doesn’t help much. The same question was asked here but the answer provided didn’t answer the question, so I’m asking it again here.
As an example:
MySQL table:
Pandas DataFrame:
Desired MySQL table:
Then for kicks, let's say we have a string column to add as well:
Desired MySQL output:
Safe to assume the index column will always match in the DF and the MySQL table.
You can use INSERT ... ON DUPLICATE KEY UPDATE.
You have the following table:
create table tbl (
index_ int ,
col_1 int ,
primary key index_(`index_`)
) ;
insert into tbl values (1,1), (2,2), (3,3), (4,4), (5,5);
And want to add the following data in a new column on the same table ;
(1,0.1),(2,0.2),(3,0.3),(4,0.4),(5,0.5)
First you need to add the column with the alter command,
alter table tbl add column col_2 decimal(5,2) ;
Then use INSERT ON DUPLICATE KEY UPDATE Statement
INSERT INTO tbl (index_,col_2)
VALUES
(1,0.1),
(2,0.2),
(3,0.3),
(4,0.4),
(5,0.5)
ON DUPLICATE KEY UPDATE col_2=VALUES(col_2);
Fiddle

In Django is it possible to Add blank row in table B if table A has duplicate values?

I have two tables in my Django project both displayed side by side. Currently those are comparing 1st columns of each and displaying matching values in front of each other along with the rest of the row.
Is it possible to somehow create a blank tr (table row) in table B if in table A a value appears twice or in first column and in table B it is only once?
The blank tr in table B should appear in front of the duplicate value in table A, so that all other rows in table B shift down and rest of the matching values be compared to each other.
This picture shows what is currently happening and what i need.

Changing pandas DataFrame values based on values from the same and previous rows

I have the following pandas df:
it is sorted by 'patient_id', 'StartTime', 'hour_counter'.
I'm looking to perform two conditional operations on the df:
Change the value of the Delta_Value column
Delete the entire row
Where the condition depends on the values of ParameterID or patient_id in the current row and the row before.
I managed to do that using classic programming (i.e. a simple loop in Python), but not using Pandas.
Specifically, I want to change the 'Delta_Value' to 0 or delete the entire row, if the ParameterID in the current row is different from the one at the row before.
I've tried to use .groupby().first(), but that won't work in some cases because the same patient_id can have multiple occurrences of the same ParameterID with a different
ParameterID in between those occurrences. For example record 10 in the df.
And I need the records to be sorted by the StartTime & hour_counter.
Any suggestions?

Copy data from one table to another table in Postgres which contains many Columns

I want to copy data from Table A to Table B in Postgres. Table A contains 40 columns and Table B contains 20 columns. It's like Table B is the subset of Table A, which means - Table B contains only some columns which are in Table A.
I have found the answer https://stackoverflow.com/a/7483174/12556735 for copying the data if there are less number of columns.
Since there are many columns , is there any way in which we can copy data without mentioning the Column names?
Choice 1:
If you notice, your referred question / answer itself answers your question as does not have any limitation on the number of columns. But you should know the original column definition and the required column list. It can run well for many columns.
Choice 2:
If the columns are unknown, you shall try using
create table 'newtable' as (select * from 'existingtable');
Choice 3:
If the columns are unknown and you wanted to create a new table with selected columns ! (which means you should know about columns), you shall try
select * from information_schema.columns where table_schema= 'yourdatabase' and table_name= 'yourtable';
And among the columns list, you shall use column_name, column_type, is_nullable, etc can be used on your script.

Change the column name of dataframe at runtime

I am trying to initialize an empty dataframe with 5 column values. Say column1, column2, column3, column4, column5.
Now I want to read data from database and want to insert specific column values from the database to this dataframe. Since there are 5 columns its easier to do it individually. But i have to extend the number of columns of the dataframe to 70. For that I am using for loop.
To update the coulmn value I was using
dataframe['column "+count+"'] = .... where count is an incremental variable ranging upto 70.
But the above code adds a new column to the dataframe. How can I use the count variable to access these column names?
i would recommend just using pandas.io.sql to download your database data. it returns your data in a DataFrame.
but if, for some reason, you want to access the columns, you already have your answer:
assignment: df['column%d' % count] = data
retrieval: df['column%d' % count]

Categories