This repository contains python codes that I have been using frequently for 4CE projects. The purpose of this repository is for my convenience :)
Long to wide:
df = pd.melt(df, id_vars=['siteid', 'lab', 'period', 'length'], value_vars=days, var_name='day', value_name='value')
Sort by value:
df = df.sort_values(by=['siteid'])
Append vertical:
meta.append(nometa)
Append custome data rows:
d = d.append({'x': 0, 'y': None, 'lab': lab, 'siteid': 'META_GERMANY', 'day': 'day1', 'period': 'early'}, ignore_index=True)
Filter by null checking:
pd.isnull(df.var2)
Filter by str contains:
df[df['A'].str.contains("hello")]
Concat strings in two columns:
df['siteid-phase'] = df.siteid + df.phase.astype(str)
Two columns to dict:
pd.Series(df.Letter.values,index=df.Position).to_dict()
Channel:
alt.X("day:Q", title=None, bin=alt.Bin(maxbins=20), axis=alt.Axis(labelAngle=0, tickCount=3), scale=alt.Scale(clamp=True)),
Text:
text = points.mark_text(
align='left',
baseline='middle',
dx=7
).encode(
text='label'
)
Axis offset:
axis=alt.Axis(titleX=-40)
Bar offset:
mark_bar(xOffset=-10)
Alternative error bars (mark_errorbar
):
mark_bar(
size=1,
color='black',
xOffset=-10
).encoding(
x=alt.X(...),
y=alt.Y(...),
y2=alt.Y(...),
)
Colors:
color=alt.Color('symbol', scale=alt.Scale(scheme='category20')),
Filters:
.transform_calculate(
order="{'Low Risk':0, 'Medium Risk': 1, 'High Risk': 2}[datum.variable]"
).transform_filter(
{'field': 'metric', 'oneOf': [metric]}
)
Log scale:
alt.Chart(df).transform_filter(
alt.datum.foos > 0
).mark_bar().encode(
alt.X('foos', scale=alt.Scale(type='log')),
y='group'
)
Use multi-line axis title:
y=alt.Y('mean:Q', scale=alt.Scale(zero=False), title=['All Patients' if patient_group == 'all' else "Ever Severe Patients", 'Mean Lab Value'] if i == 0 else None, axis=alt.Axis(titleX=titleX)),
Dashed lines:
alt.Chart(source).mark_line().encode(
x='date',
y='price',
color='symbol',
strokeDash='symbol',
)