I have a pandas df like this
a column
_____
NaN
e
NaN
b
I want to save the count of a non-nan values in a new dataframe:
df_ new=pd.DataFrame()
df_new['column1']= int(df["a column"].count())
My expected output is:
df_new
column1
2
but nothing happens. Why?
CodePudding user response:
df = pd.DataFrame(
{
'col1':[np.nan,'e',np.nan,'b']
}
)
df
df2 = df.count()
df2
It's series
.
We can check the type of df2
type(df2)
<class 'pandas.core.series.Series'>
You could turn it into DataFrame
by to_frame()
df3 = df.count().to_frame()
df3
type(df3)
<class 'pandas.core.frame.DataFrame'>