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.

pivot_table function with renaming columns

See original GitHub issue

Here is the example from the documentation.

df = ks.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
                          "bar", "bar", "bar", "bar"],
                    "C": ["small", "large", "large", "small",
                          "small", "large", "small", "small",
                          "large"],
                    "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]},
                   columns=['A',  'C',  'E'])
df.pivot_table(values='E', index=['A'],
                        columns='C', aggfunc='sum')

The above code works as expected. Then I rename the columns like

df.columns =['New_A','New_C','New_E']
print(df.columns)

This works fine with applymap and other functions like to_datetime but as soon as I use pivot_table , I got an error.

df.pivot_table(values='New_E', index=['New_A'],
                        columns='New_C', aggfunc='sum')

The above code will give the following error:


KeyError: 'No StructField named New_E'

Is it not allowed to change the column name right now?? Thanks.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
harupycommented, Sep 20, 2019

I’ll work on this.

1reaction
ueshincommented, Sep 20, 2019

Shall we fix the columns setter to rename the Spark columns for now? @itholic @harupy Do you want to fix it?

I’ll handle merge and pivot_table soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rename a table or field in Power Pivot - Microsoft Support
In the Power Pivot window, right-click the tab that contains the table that you want to rename, and then click Rename. For columns,...
Read more >
Quick tip: Rename headers in pivot table so they are ...
Just type over the headers / total fields to make them user friendly. See this quick demo to understand what I mean: So...
Read more >
How to rename fields in a pivot table - Exceljet
You can do this by accessing Field Settings for the field in question. Either right-click on the field and choose Value field settings,...
Read more >
How to Modify Column Names in Pandas Pivot Table - Statology
... or format the column names in a pandas pivot table in a specific way. Fortunately this is easy to do using built-in...
Read more >
How to Rename Pandas DataFrame Columns (with Examples)
This method is best when you want to rename all columns following the same ... Say we create a Pandas pivot table and...
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