Home > Mobile >  Concatenate two columns in pandas
Concatenate two columns in pandas

Time:08-16

Good evening, I need help on getting two columns together, my brain is stuck right now, here's my code:

import pandas as pd
import numpy as np
tabela = pd.read_csv('/content/idkfa_linkedin_user_company_202208121105.csv', sep=';')
tabela.head(2)
coluna1 = 'startPeriodoMonth'
coluna2 = 'startPeriodoYear'
pd.concat([coluna1, coluna2])

ERROR: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid

I'm currently getting this error, but I really don't know what to do, by the way I'm a beginner, I don't know much about coding, so any help is very appreciated.

CodePudding user response:

I am new to Pandas too. But I think I can help you. You seem to have created (2) string variables by encapsulating the literal strings "startPeriodMonth" and "startPeriodYear" in single quotes ('xyz'). I think that what you're trying to do is pass columns from your Pandas data frame... and the way to do that is to explicitly reference the df and then wrap your column name in square brackets, like this:

coluna1 = tabela[startPeriodoMonth]

This is why it is saying that you "can't concatenate an object of type string". It only accepts a series or dataframe object.

CodePudding user response:

From what I understand coluna1, coluna2 are columnw from tabela. You have two options:

The first is selecting the columns from the dataframe and storing it in a new dataframe.

import pandas as pd
import numpy as np
tabela = pd.read_csv('/content/idkfa_linkedin_user_company_202208121105.csv', sep=';')
tabela.head(2)
coluna1 = 'startPeriodoMonth'
coluna2 = 'startPeriodoYear'
new_df=df[[coluna1, coluna2]]

The second option is creating a Dataframe, which contains just the desired column (for both columns), followed by the concatenation of these Dataframes.

coluna1 = 'startPeriodoMonth'
coluna2 = 'startPeriodoYear'
df_column1=tabela[[coluna1]]
df_column2=tabela[[coluna2]]

pd_concat=[df_column1, df_column2]
result = pd.concat(pd_concat)
  • Related