I have a Dataframe where that mix String, Date and float data. Now I would like to be able to sum all columns in my dataframe but only for specific rows where only floats are available. Like the following
How can I do the following ? I tried df1['sum']= df1.sum(axis=1, skipna=True, numeric_only=True) but the output is 0 for each rows here is my code :
import pandas as pd
import streamlit as st
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go
from plotly import tools
#%%
import requests
from ValueAppMethods import get_full_data, investorData, get_stock_description, get_historical_market_Cap, get_historical_pe,get_price_ratio, get_ROIC_nopat
st.set_page_config(page_title="ValueApp", layout="wide", initial_sidebar_state="collapsed")
text_input = '1810.hk'
data = get_full_data(text_input)
is_data = investorData(data, "Income Statement")
#%%
ttm_data = {}
for i,j in data['Financials']['Income_Statement']['quarterly'].items():
ttm_data[i] = j
df = pd.DataFrame(ttm_data).T
df['formatted_date'] = pd.to_datetime(df['date'])
df.sort_values(by='formatted_date', ascending=True, inplace=True)
not_wanted = ["date", "filing_date", "currency_symbol", "formatted_date"]
for i in list(df.columns):
if i not in not_wanted:
df[i] = df[i].astype(float)
df = pd.DataFrame(df).T
df1 = df.iloc[:,-4:].fillna(0)
df1['sum']= df1.sum(axis=1, skipna=True, numeric_only=True)
CodePudding user response:
You can use:
df1['sum'] = df1.T.apply(pd.to_numeric, errors='coerce').dropna(how='all', axis=1) \
.sum().reindex(df1.index, fill_value='-')
print(df1)
# Output
Col1 Col2 Col3 sum
row1 test test test -
row2 2 3 5 10
row3 4 5 6 15