column insertion via python series

Asked

Viewed 29 times

1

I have a series collected from a column of dataframe. I need to create columns with each of this series. For example:

serie = []
    for s in serie:
        df.insert(loc=0,column=serie,value='int')
  • columns: A B C D E
  • series: F G H I J
  • after insertion: A B C D E F G H I J

I couldn’t use the insert. What other way could be used to create a column of each series result?

1 answer

1


Inserting columns into a dataframe can be done in two ways.

Creating new columns:

df = pd.DataFrame()
serie = 'F G H I J'.split(' ')
for s in serie:
    df[s] = []

print(df)

Merging from another bank:

df = pd.DataFrame(columns='A B C D E'.split(' '))
df2 = pd.DataFrame(columns='F G H I J'.split(' '))
print(df.join(df2))

Note that index 0 is the index of the first observation, not the column name. Hence the error in your code.

  • thanks, all right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.