0

I am using Pandas to merge two dataframes obtained from Psychopy.

y = ["key_resp_0.keys", "key_resp_0.rt"] #some columns I want in my final dataframe
df = pd.DataFrame(myData)
columns = df.columns.values.tolist() 
df2 = df.reindex(columns = y, fill_value='')
df3 = pd.merge(df2,df)

This is the error I get:

type object argument after * must be an iterable, not itertools.imap

I checked what type of dataframes I have:

print(type(df))
print(type(df2))

This is the result:

<class 'pandas.core.frame.DataFrame'> #df
<class 'pandas.core.frame.DataFrame'> #df2

Following some info found in other posts, I also tried to convert df and df2 into tuples and then doing the merge.

df = df.apply(tuple) 
df2 = df2.apply(tuple)
df3 = pd.merge(df2,df)

Then I get a different error

can not merge DataFrame with instance of type <class 'pandas.core.series.Series'>

Do you know what could be a way to merge these two dataframes?

giorgio-p
  • 91
  • 1
  • 5
  • in merging, you have to decide which column to be merged. do you want to merge you dataframes or append one at the end of the other? – prhmma Dec 03 '19 at 14:23
  • This is what I would like to do: 1) make a copy of `df` as `df2`, where `df2` has the columns in the order I specified in `y `. However, some columns in `df` are not specificed in `y`, because I can not know them in advance. So I want to: 2) Add those columns that are in df but not in `y` at the end of `df2`, by creating `df3`. In a schema. df3 = df2 + (df -y), in terms of columns. – giorgio-p Dec 03 '19 at 14:37
  • Does this answer your question? [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – Andrea Dec 03 '19 at 15:02
  • what you want is not merging, by creating a copy of your original df, and add extra columns, your new df as df2 is what you want without any other steps – prhmma Dec 03 '19 at 17:54

2 Answers2

0

Check this thread. Literally, I see this question asked every few days. Official docs are rather covering this as well. RTFM bro.

Piotr Rarus
  • 884
  • 8
  • 16
  • Thanks for the reply, I checked the thread and the docs but I don´t see any mention of the first error I am reporting. I will look at these links more deeply though. – giorgio-p Dec 03 '19 at 14:53
0

I think I managed to find a solution.

In my dataframe df there was a possibly empty string (when opening df as a .csv file, one cell contained only []). Now I am assigning to that cell a value of zero and the merge function seems to work properly.

giorgio-p
  • 91
  • 1
  • 5