Home > other >  Trying to save a number in a new dataframe but nothing happens (Pandas)
Trying to save a number in a new dataframe but nothing happens (Pandas)

Time:07-08

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

enter image description here




df2 = df.count()
df2

enter image description here

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

enter image description here

type(df3)
<class 'pandas.core.frame.DataFrame'>
  • Related