Home > database >  Adding a column into dataframe that is based on the number of another dataframe
Adding a column into dataframe that is based on the number of another dataframe

Time:03-25

Hey guys I have the following issue following a code that would help me do the following.

I have this dataframe (is based on a the max column of MSSQL table that functions as an index there, data is already downloaded and passed to a df):

last_row
39021

And I have the following Data frame that was created by consuming several CSV's and other sources:

blank_col random column1 random column2
asgshg2342342d testdata1
asert54363546 testdata2

As you can see the first column is blank and need to insert the index based on the first dataframe so the final product with the column inserted should look like this:

blank_col random column1 random column2
39022 asgshg2342342d testdata1
39023 asert54363546 testdata2

This is the code that I've been trying and gives me an error

last_row_counter = df1.last_row.to_list()

n = int(float(input(last_row)))
df2['column_Id'] = n   1

So basically just inserting the last_row 1 each row on the second dataframe

Any assistance with this will be much appreciate. PD: apologize for my English is not my first language.

CodePudding user response:

In you case

df2['column_Id'] = np.arange(len(df2))   lastrowfromdf1   1

CodePudding user response:

You can use squeeze to the the value from a Series if there're only one, and then create a range and add it to the number:

df2['blank_col'] = df1['last_row'].squeeze()   np.arange(df2.shape[0]) 1

Output:

>>> df2
   blank_col  random column1 random column2
0      39022  asgshg2342342d      testdata1
1      39023   asert54363546      testdata2
  • Related