Suppose that I have a pandas dataframe, df1:
import pandas as pd
df1col = ['col1', 'col2']
df1 = pd.DataFrame(columns=df1col)
df1.loc[0] = 'a', 'b'
My goal is to create df2 where the first two columns of df2 are the same as those in df1. I use the names of the columns and append the column names of col3 and col4 on (is there a better way to do this?) and create the dataframe, df2:
df2col = df1col
df2col.append('col3')
df2col.append('col4')
df2 = pd.DataFrame(columns=df2col)
Now I simply want to add the first (and only) row of df1 to the first row of df2 AND I want to add two new entries (c and d) so that all the columns are filled. I've tried:
df2.loc[0] = df1.loc[0], 'c', 'd'
and
df2.loc[0] = [df1.loc[0], 'c', 'd']
But neither works. Any hints?
df2, which has column names fromdf1plus two new column names. Now I want to create one row indf2that takes the values from row 0 ofdf1for the first two columns (aand thenb). The third column will becand the fourthd.