Home > database >  Pandas - Adding rows from one dataframe to the another
Pandas - Adding rows from one dataframe to the another

Time:10-20

I am trying to add manually row (one by one) from one dataframe to the another:

path = "...csv"

data = pd.read_csv(path, na_values='NULL')

path2 = "...csv"

data2 = pd.read_csv(path2, na_values='NULL')

for row in data2.itertuples():

     x = input("Do you want to add the row, please write Yes or No")

     if x=='Yes':

          data = data.append(row)

          print(data.shape)

     else:

         pass

Shape of those dataframes are: (1674, 83) (1727, 83). When I run this code I receive this error:

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

Any ideas how to fix it? Can't find a good solution for it.

Thanks for your help and cheers!

CodePudding user response:

DataFrame.itertuples returns a namedtuple, you can convert it to dictionary with ._asdict() and create a one row DataFrame

df1 = df1.append(pd.DataFrame([row._asdict()]).set_index('Index'))

CodePudding user response:

You need to pass a Dataframe or Seires to the append method.

So you can either convert every row in a Series and then append, like Ynjxsjmh said:

DataFrame.itertuples returns a namedtuple, you can convert it to dictionary with ._asdict() and create a one row DataFrame

df1 = df1.append(pd.DataFrame([row._asdict()]).set_index('Index'))

or you can store all the result you want to append in a list ant then create a Dataframe from that list and append it all at once.

path = "...csv"

data = pd.read_csv(path, na_values='NULL')

path2 = "...csv"

data2 = pd.read_csv(path2, na_values='NULL')

result = list()

for row in data2.itertuples():

    x = input("Do you want to add the row, please write Yes or No")

    if x == 'Yes':

        result.append(row)

        print(data.shape)

df1 = pd.DataFrame(result)

data = pd.concat(data, df1)
  • Related