Code Monkey home page Code Monkey logo

altair-frequently-used-code's Introduction

altair-frequently-used-code

This repository contains python codes that I have been using frequently for 4CE projects. The purpose of this repository is for my convenience :)

Pandas

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()

Altair

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',
)

altair-frequently-used-code's People

Contributors

sehilyi avatar

Stargazers

Mark Keller avatar Andrew McNutt avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.