Ad
How To Count Length Of Missing Values For String Variables As Zero?
I'm trying to count the length of object variables in a dataframe with Python. A lot of my variables are string with missing values and unfortunately when I try to count the length of missing values it shows as 3 (as it counts "Nan" as a 3 character value).
Here's the code that I'm using:
df_string_mean_with_na = pd.DataFrame(df_string.applymap(len).astype(int).mean().to_dict(), index=[df_string.index.values[0]])
where df_string is my starting dataframe and I'm trying to calculate the average length of values for each columns. I would like to count the length of missing values for object variables as 0, is there a way?
Ad
Answer
I think you need DataFrame.fillna
for replace missing values to empty strings before counting length
:
print (Table1)
A B C
0 hello hi NaN
1 good hi so
2 home hello no
Test missing values:
print (Table1.isna())
A B C
0 False False True
1 False False False
2 False False False
df = Table1.fillna('').applymap(len).mean().to_frame().T
print (df)
A B C
0 4.333333 3.0 2.333333
Detail:
print (Table1.fillna('').applymap(len))
A B C
0 5 2 0
1 4 2 2
2 4 5 2
If missing values are strings
use DataFrame.replace
:
print (Table1.isna())
A B C
0 False False False
1 False False False
2 False False False
df = Table1.replace('NaN', '').applymap(len).mean().to_frame().T
print (df)
A B C
0 4.333333 3.0 2.333333
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad