Can't convert dataframe from float64 to string to compare values using ISIN


Can't convert dataframe from float64 to string to compare values using ISIN



I am reading in one column of data as a dataframe from two different Excel worksheets (df1, df2), so that I can use the ISIN command to determine what values in df1 are missing from df2. The values are for example like 4533.01, and 3578.05. They are actually string values but the dataframe info reports that they are stored as float64 values.



When I performed an ISIN command, I got FALSE for all 1000+ entries. So I figured that I would change the dataframe to a string using


df1['cola'] = df1['cola'].astype(str)



but the dataframe still remains as a float64.



I looked at this post; pandas - change df.index from float64 to unicode or string. but it did not help me too much.



Why do i need to do convert the dataframe to a string or any other method, so that I can properly use ISIN on the two dataframes?
Each dataframe has only 1 column of data.



Data sample from df1


In [51]: df1
Out[51]:
ASno
0 122.04
1 122.01
2 122.03
3 122.02
4 587.01
5 587.02
6 587.03
7 587.04
8 624.02

In[52] df1.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1060 entries, 0 to 1059
Data columns (total 1 columns):
ASno 1060 non-null float64
dtypes: float64(1)
memory usage: 8.4 KB



Additional info:


df1 = pd.read_excel('tryout.xlsx',sheet_name='Sheet1')
df2 = pd.read_excel('tryout.xlsx',sheet_name='Sheet2')

# df3 = df1.isin(df2.to_dict('l'))
df3 = [df1.isin(df2).all(axis=1)]
df3A = pd.DataFrame(df3)
df4 = df3A.T

In [102]: df1
Out[102]:
ASno
0 122.04
1 122.01
2 8816.01

In [102]:df2
Out[102]:
TPaspects
0 8816.01
1 122.04
2 8978.02
3 4673.03

In [104]: df4
Out[104]:
0
0 False
1 False
2 False



Yet when I replace the line of code as such:


df3 = [df2.isin(df2).all(axis=1)]



All results come back as True. Therefore, I did not expect to get all False when I use df2 compared to df1.



Update: Fixed!!!
After reading df1, df2 from Excel, i executed the following lines of code:


df1a = df1['ASno'].apply(pd.Series)
df2a = df2['TPaspects'].apply(pd.Series)
df3 = df1a.isin(df2a.to_dict('l'))



Then I continued with the same line of code to convert to dataframee df3A, and it worked. I get True, False values respectively in my rows. The .apply(Pd.Series) and using isin with dict made all the difference.


df3A = pd.DataFrame(df3)





Can you add data sample?
– jezrael
Jun 30 at 19:13





Data sample added
– GusG
Jun 30 at 19:24





Your question is not clear. Please provide a Minimal, Complete, and Verifiable example, for example I can't reproduce your problem on the conversion not working.
– coldspeed
Jun 30 at 19:59





Corrected typo in line of code for df3, just before the last sentence of my post.
– GusG
Jun 30 at 21:04





@coldspeed In my post I have added sample line of code under Additional info. – GusG 20 mins ago
– GusG
Jun 30 at 21:09









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter