question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

transform_fold() does not work after renaming columns

See original GitHub issue

Running

d1_coll = alt.hconcat()

d1_bvb = bvb.copy()
d1_bvb.rename(columns = {'value' : 'value_renamed', 'fee' : 'fee_renamed'}, inplace=True)

for chart in [
    [0 , 'All players'], 
    [1, 'Excluding free agents'], 
    [1000000, 'Transfers above €1 million']
    ]: 
    
    d1_chart = alt.Chart(
        d1_bvb,
        title = chart[1]
        
    ).transform_filter(
        datum.fee >= chart[0]
        
    ).transform_fold(
        ['value_renamed', 'fee_renamed'], as_ = ['key_fold', 'value_fold']
        
    ).mark_bar(
        clip=True
    ).encode(
        y=alt.Y('mean(value_fold):Q', scale=alt.Scale(domain=(0,14000000)), title='Average in (€)'),
        x=alt.X('key_fold:N', title = 'x-Axis title'),
        color=alt.Color('key_fold:N'),
        column=alt.Column('trans_type', title='column title', spacing=5)
    )
    
    d1_coll |= d1_chart
    
d1_coll

returns no chart.

When I comment out the d1_bvb.rename() line and change the first argument in transform_fold() to value and fee, it returns the correct chart.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jakevdpcommented, Feb 22, 2020

The only way to change the names of your columns once they’re passed to the chart is to use a calculate transform. You could do something like this:

import numpy as np
import pandas as pd
import altair as alt

rand = np.random.RandomState(0)
data = pd.DataFrame({
    'date': pd.date_range('2019-01-01', freq='D', periods=30),
    'data_a': rand.randn(30).cumsum(),
    'data_b': rand.randn(30).cumsum(),
    'data_c': rand.randn(30).cumsum(),
})

alt.Chart(data).transform_calculate(**{
    'Data A': 'datum.data_a',
    'Data B': 'datum.data_b',
    'Data C': 'datum.data_c',
}).transform_fold(
    ['Data A', 'Data B', 'Data C'],
).mark_line().encode(
    x='date:T',
    y='value:Q',
    color='key:N',
)

visualization - 2020-02-22T062727 596

0reactions
yannxavercommented, Feb 26, 2020

Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Renaming the column names of pandas dataframe is not ...
I am trying to rename the column names but it not working as expected. Code: mapping = {df.columns[0]:'Date', df.columns[1]: ...
Read more >
Renaming columns -> new name not showing for site members
Hi community, I noticed that in a recent created SharePoint Online site, that when I rename columns, I (as creator) see the new...
Read more >
Renaming a field in the dataset - AWS Glue Studio
You can use the RenameField transform to change the name for an individual property key in the dataset.
Read more >
pandas.DataFrame.rename — pandas 1.5.2 documentation
If 'raise', raise a KeyError when a dict-like mapper , index , or columns contains labels that are not present in the Index...
Read more >
R dplyr & plyr Error: Can't rename columns that don't exist. (2 ...
Let's start right away. Creation of Example Data. We'll use the following data as basement for this R programming tutorial: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found