0

I have two dataset,

df1

100

20


30


5





df2

3

4


5

6

When i try to merge df3=pd.concat([df1, df2], axis=1, ignore_index= False)

I get the output as :

df1  df2

100.0  3
20.0   4
30.0   5
5.0    6

It gets automatically changed to float, and .astype("Int64"), doesnot change the float, is there any way to avoid this?

Coder
  • 67
  • 7
  • `df1` and `df2` are probably not the same length, so the sorter row is filled with `np.nan` and `np.nan` can't be converted by `.astype(int)`. You can `.dropna` or `fillna` and then change the type. – Trenton McKinney Jul 10 '22 at 18:56

3 Answers3

1

You can try and see:

df3.astype(int)
Noob Coder
  • 202
  • 3
  • 16
0

Unable to reproduce your output

df1 = pd.DataFrame({"a": [100,20,30,5]})
df2 = pd.DataFrame({"b": [3,4,5,6]})
df3=pd.concat([df1, df2], axis=1, ignore_index= False)
print(df3)

     a  b
0  100  3
1   20  4
2   30  5
3    5  6
Rajesh
  • 766
  • 5
  • 17
0

Can you add the output of df1.info() and df2.info()?
And print(df1.to_csv(index=False)) and print(df2.to_csv(index=False))?

That helps in reproducing the issue.

Seems to work fine without further details:

df1 = pd.DataFrame({'A':[100,20,30,5]})
df2 = pd.DataFrame({'B':[3,4,5,6]})
df3 = pd.concat([df1, df2], axis=1, ignore_index= False)
print(df3)

Result:

     A  B
0  100  3
1   20  4
2   30  5
3    5  6
René
  • 4,594
  • 5
  • 23
  • 52