Skip to content Skip to sidebar Skip to footer

Concatenate Two Dataframes Based On No Of Rows

I have two dataframes: a b c d e f 2 4 6 6 7 1 4 7 9 9 5 8 7 9 6 5 8 2 Now I want to create a new dataframe like this: a b c d e f 2 4 6 6 7 1 4

Solution 1:

concat have join = 'inner'

pd.concat([x,y],join = 'inner', axis=1)
Out[184]: 
   abcdef02466711479958

Solution 2:

import pandas as pd
df1=pd.DataFrame({'a': [2, 4], 'b': [4, 7], 'c': [6, 9]})
df2=pd.DataFrame({'d': [6, 9, 7, 5], 'e': [ 7, 5, 9, 8], 'f': [1,8,6, 2]})
result = pd.concat([df1,df2],axis=1, join_axes=[df1.index])
print(result)

Refer panda documentation. Its really good.Added the link below.

https://pandas.pydata.org/pandas-docs/stable/merging.html

Solution 3:

>>>import pandas as pd>>>x = pd.DataFrame({'a': [2, 4], 'b': [4, 7], 'c': [6, 9]})>>>y = pd.DataFrame({'d': [6, 9, 7, 5], 'e': [ 7, 5, 9, 8], 'f': [1,8,6, 2]})>>>x
   a  b  c
0  2  4  6
1  4  7  9
>>>y
   d  e  f
0  6  7  1
1  9  5  8
2  7  9  6
3  5  8  2

>>>pd.concat([ x, y], axis=1).dropna()
     a    b    c  d  e  f
0  2.0  4.0  6.0  6  7  1
1  4.0  7.0  9.0  9  5  8

Solution 4:

Using pandas.concat and slicing we can proceed by doing:

a = pd.DataFrame(np.random.randint(10, size=(2,3)), columns=["a", "b", "c"])
b = pd.DataFrame(np.random.randint(10, size=(4,3)), columns=["d", "e", "f"])

So the two DataFrames look like:

a

b

Then just run:

result = pd.concat([a, b[:len(a)]], axis=1)

So the result is:

result

Solution 5:

You can use join to join the dataframes

df1 = pd.DataFrame({'a': [2, 4], 'b': [4, 7], 'c': [6, 9]})
df2= pd.DataFrame({'d': [6, 9, 7, 5], 'e': [ 7, 5, 9, 8], 'f': [1,8,6, 2]})
df1.join(df2)

   a  b  c  d  e  f
02466711479958

Post a Comment for "Concatenate Two Dataframes Based On No Of Rows"