1

My code is given below: I have two data frames a,b. I want to create a new data frame c by merging a specific index data of a, b frames.

import pandas as pd
a = [10,20,30,40,50,60]
b = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
a = pd.DataFrame(a,columns=['Voltage'])
b = pd.DataFrame(b,columns=['Current'])
c = pd.merge(a,b,left_index=True, right_index=True)
print(c)

The actual output is:

     Voltage  Current
0       10      0.1
1       20      0.2
2       30      0.3
3       40      0.4
4       50      0.5
5       60      0.6

I don't want all the rows. But, specific index rows something like:

c =      Voltage Current
     0     30      0.3
     1     40      0.4

How to modify c = pd.merge(a,b,left_index=True, right_index=True) code so that, I only want those specific third and fourth rows in c with new index order as given above?

Msquare
  • 293
  • 1
  • 3
  • 15

1 Answers1

2

Use iloc for select rows by positions and add reset_index with drop=True for default index in both DataFrames:

Solution1 with concat:

c = pd.concat([a.iloc[2:4].reset_index(drop=True),
               b.iloc[2:4].reset_index(drop=True)], axis=1)

Or use merge:

c = pd.merge(a.iloc[2:4].reset_index(drop=True),
             b.iloc[2:4].reset_index(drop=True), 
             left_index=True, 
             right_index=True)

print(c)
   Voltage  Current
0       30      0.3
1       40      0.4
jezrael
  • 629,482
  • 62
  • 918
  • 895
  • Excellent. It worked. Could you please, explain the significance or meaning of drop=True, axis=1 in concat code? Also, could you explain the significance or meaning of left-Index=True,right_index=True in Merge code? – Msquare Aug 14 '18 at 09:00
  • OK, the best for `drop=True` is try remove it like `c = pd.concat([a.iloc[2:4], b.iloc[2:4]], axis=1)`, then get not `0,1` index but `2,3` – jezrael Aug 14 '18 at 09:02
  • Ok. What about `a.iloc'? I heard about `a.loc`? I mean, `loc` and `iloc` are different? – Msquare Aug 14 '18 at 09:04
  • `axis=1` in `concat` mean you want join by columns, check [docs](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.concat.html) `The axis to concatenate along` and similar for `merge` it join by both indexes. – jezrael Aug 14 '18 at 09:04
  • @Msquare - I think better is is explained [here](https://stackoverflow.com/questions/31593201/pandas-iloc-vs-ix-vs-loc-explanation-how-are-they-different) – jezrael Aug 14 '18 at 09:05
  • You mean, in `merge` code, `left_index` refers to `a` and `right_index` refers to `b`? Am I right in saying that? – Msquare Aug 14 '18 at 09:05
  • @Msquare - merging by `index` is explained [here](https://stackoverflow.com/a/40468090/2901002) with sample data for difference. – jezrael Aug 14 '18 at 09:07
  • Thanks for educating me. I learned about `merge` code objects here. I have gone through `index` based merging which is explained by you at some other reference. There you have used `abcdef`, `abhi` etc as index. I was wondering!, instead `abcdef' how to mention integers ? I mean if the list is big, we don't the number of rows? – Msquare Aug 14 '18 at 09:16
  • In my code with similar merging, i got this error: `ValueError: can not merge DataFrame with instance of type ` What could be the reason? – Msquare Aug 14 '18 at 09:19
  • @Msquare - You want merge by `Series`, then need `df = df.to_frame()`, check it by `print (type(df))` – jezrael Aug 14 '18 at 09:20
  • My merge code: `filteredIV_df = pd.merge(v.iloc[u1:u2].reset_index(drop=True),i.iloc[u1:u2].reset_index(drop=True),left_index=True,right_index=True)` Error is: `ValueError: can not merge DataFrame with instance of type `. I checked `print(type(v))` and it gives` Empty DataFrame Columns: [] Index: []` – Msquare Aug 14 '18 at 09:27
  • Excellent. Your are great. It worked perfectly. I have been struggling with this for a while. I am wondering! how much time you took to learn Python or to reach this stage? What was your source to learn the Python? I want to take cues from your expertise. – Msquare Aug 14 '18 at 09:31
  • @Msquare - For me the best is `StackOverflow` for learning and [tutorials](http://pandas.pydata.org/pandas-docs/stable/tutorials.html) – jezrael Aug 14 '18 at 10:08