OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python seems to return incorrect data length for NULL value from a Pandas dataframe

  • Thread starter Thread starter szheng
  • Start date Start date
S

szheng

Guest
I am trying to find the longest value and its length from a column in a Pandas dataframe.

This column holds country codes which are strictly 2 charterers without any empty strings or spaces, but data could be missing.

For example, data looks like this:

Code:
col
US
RU
None

My codes look like this:

Code:
max_data_len = df_copy[col].astype(str).str.len().max()
longest_value = df_copy[col].iloc[df_copy[col].astype(str).str.len().idxmax()]
print(max_data_len)
print(longest_value)

While the output is

Code:
4
None

where I am expecting the results to be

Code:
2
'US'

Could you suggest what else I need to do to achieve the above, and also is there a way to return the longest value and its length in a single line?
<p>I am trying to find the longest value and its length from a column in a Pandas dataframe.</p>
<p>This column holds country codes which are strictly 2 charterers without any empty strings or spaces, but data could be missing.</p>
<p>For example, data looks like this:</p>
<pre><code>col
US
RU
None
</code></pre>
<p>My codes look like this:</p>
<pre><code>max_data_len = df_copy[col].astype(str).str.len().max()
longest_value = df_copy[col].iloc[df_copy[col].astype(str).str.len().idxmax()]
print(max_data_len)
print(longest_value)
</code></pre>
<p>While the output is</p>
<pre><code>4
None
</code></pre>
<p>where I am expecting the results to be</p>
<pre><code>2
'US'
</code></pre>
<p>Could you suggest what else I need to do to achieve the above, and also is there a way to return the longest value and its length in a single line?</p>
 

Latest posts

Top