0
$\begingroup$

I am trying to insert a column of values from one dataframe to another. The total count of values of both the indiviual dataframes is 91744 but when I use

df1.insert(loc=0, column='x1', value=ts)

to insert the values of ts as the first column in df1, the value count after insertion turns out to be 84175 instead of actual 91744. Please tell me what is wrong with this.

>>> df1.insert(loc=0, column='x1', value=ts)
>>> df1.x1.count()
84175
>>> ts.count()
0    91744
dtype: int64

>>> df1.describe()
                 x1            x2
count  84175.000000  91744.000000
mean       0.249044     11.744387
std        0.191239     13.294185
min        0.000000      1.000000
25%        0.108383      3.000000
50%        0.212760     12.000000
75%        0.311860     15.000000
max        1.000000    551.000000

Sample data is given below.

>>> df1.head()
   x2  x3  x4   x5                   x6  y
0  12   1  13  497  2017-11-07 09:30:38  0
1  25   1  17  259  2017-11-07 13:40:27  0
2  12   1  19  212  2017-11-07 18:05:24  0
3  13   1  13  477  2017-11-07 04:58:08  0
4  12   1   1  178  2017-11-09 09:00:09  0
>>> ts.head()
         x1
0  0.218561
1  0.269357
2  0.257698
3  0.238417
4  0.164645

My objective was to add the x1 column from of ts dataframe as the first column of df1 data frame.

$\endgroup$

closed as off-topic by Toros91, Stephen Rauch, Benji Albert, Icyblade, Spacedman May 11 '18 at 11:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about data science, within the scope defined in the help center." – Toros91, Stephen Rauch, Benji Albert, Icyblade, Spacedman
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ What is ts? I am not able to reproduce the error. If it is some public dataset, can you provide it? $\endgroup$ – Ankit Seth May 9 '18 at 10:19
  • $\begingroup$ @AnkitSeth ts is the dataframe name that needs to be added to df1 dataframe at column index 0. $\endgroup$ – nainometer May 9 '18 at 10:27
1
$\begingroup$

Can you share some of your data frame i would recommend using merge instead of insert if you have a common column let's say ID for example your code will look like this:

mergedStuff = pd.merge(df1, df2, on=['id'], how='outer')
mergedStuff.head()

as simple as that

$\endgroup$
  • $\begingroup$ I have added a sample data in the main question above. $\endgroup$ – nainometer May 9 '18 at 10:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.