how to add columns label on a Pandas DataFrame

Stefano Fedele

I can't understand how can I add column names on a pandas dataframe, an easy example will clarify my issue:

dic = {'a': [4, 1, 3, 1], 'b': [4, 2, 1, 4], 'c': [5, 7, 9, 1]}
df = pd.DataFrame(dic)

now if I type df than I get

   a  b  c
0  4  4  5
1  1  2  7
2  3  1  9
3  1  4  1

say now that I generate another dataframe just by summing up the columns on the previous one

a = df.sum()

if I type 'a' than I get

a     9
b    11
c    22

That looks like a dataframe without with index and without names on the only column. So I wrote

a.columns = ['column']

or

a.columns = ['index', 'column']

and in both cases Python was happy because he didn't provide me any message of errors. But still if I type 'a' I can't see the columns name anywhere. What's wrong here?

ysearka

The method DataFrame.sum() does an aggregation and therefore returns a Series, not a DataFrame. And a Series has no columns, only an index. If you want to create a DataFrame out of your sum you can change a = df.sum() by:

a = pandas.DataFrame(df.sum(), columns = ['whatever_name_you_want'])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to filter pandas dataframe columns by partial label

From Dev

How to add columns to an empty pandas dataframe?

From Java

How to add row in pandas dataframe with None values to some columns

From Java

How to add multiple columns to pandas dataframe in one assignment?

From Dev

Python: how to add a column to a pandas dataframe between two columns?

From Dev

how to select/add a column to pandas dataframe based on a function of other columns?

From Dev

Pandas dataframe: how to apply describe() to each group and add to new columns?

From Dev

How to add multiple columns to pandas dataframe in one assignment?

From Dev

How to add numpy matrix as new columns for pandas dataframe?

From Dev

How can I add multiple variable arrays to columns in a pandas dataframe?

From Dev

How to add a y-axis label while using pandas.DataFrame.plot() for constructing a time series plot

From Dev

How do I add two new columns on the basis of the values of multiple other columns in a pandas dataframe?

From Dev

How to efficiently add multiple columns to pandas dataframe with values that depend on other columns

From Dev

How to concat Pandas dataframe columns

From Dev

How to reduce columns in dataframe pandas

From Java

Add multiple empty columns to pandas DataFrame

From Dev

Is it possible to add several columns at once to a pandas DataFrame?

From Dev

Add multiple columns to a Pandas dataframe quickly

From Dev

Add Multiple Columns to Pandas Dataframe from Function

From Dev

Add values and columns in specific order to Pandas DataFrame

From Dev

Pandas DataFrame.add() -- ignore missing columns

From Dev

How to add and compute (based on other columns) a sub-column in a pandas dataframe with hierarchical index?

From Dev

How to edit/add two columns to a dataframe in pandas at once - df.apply()

From Dev

How to group columns by label in a histogram using a panda DataFrame?

From Dev

How to Label encode multiple non-contiguous dataframe columns

From Dev

How to add a time to given columns in a dataframe

From Dev

R - How to add sequential columns to dataframe on conditional?

From Java

How to show all of columns name on pandas dataframe?

From Dev

how to split 'number' to separate columns in pandas DataFrame

Related Related

  1. 1

    How to filter pandas dataframe columns by partial label

  2. 2

    How to add columns to an empty pandas dataframe?

  3. 3

    How to add row in pandas dataframe with None values to some columns

  4. 4

    How to add multiple columns to pandas dataframe in one assignment?

  5. 5

    Python: how to add a column to a pandas dataframe between two columns?

  6. 6

    how to select/add a column to pandas dataframe based on a function of other columns?

  7. 7

    Pandas dataframe: how to apply describe() to each group and add to new columns?

  8. 8

    How to add multiple columns to pandas dataframe in one assignment?

  9. 9

    How to add numpy matrix as new columns for pandas dataframe?

  10. 10

    How can I add multiple variable arrays to columns in a pandas dataframe?

  11. 11

    How to add a y-axis label while using pandas.DataFrame.plot() for constructing a time series plot

  12. 12

    How do I add two new columns on the basis of the values of multiple other columns in a pandas dataframe?

  13. 13

    How to efficiently add multiple columns to pandas dataframe with values that depend on other columns

  14. 14

    How to concat Pandas dataframe columns

  15. 15

    How to reduce columns in dataframe pandas

  16. 16

    Add multiple empty columns to pandas DataFrame

  17. 17

    Is it possible to add several columns at once to a pandas DataFrame?

  18. 18

    Add multiple columns to a Pandas dataframe quickly

  19. 19

    Add Multiple Columns to Pandas Dataframe from Function

  20. 20

    Add values and columns in specific order to Pandas DataFrame

  21. 21

    Pandas DataFrame.add() -- ignore missing columns

  22. 22

    How to add and compute (based on other columns) a sub-column in a pandas dataframe with hierarchical index?

  23. 23

    How to edit/add two columns to a dataframe in pandas at once - df.apply()

  24. 24

    How to group columns by label in a histogram using a panda DataFrame?

  25. 25

    How to Label encode multiple non-contiguous dataframe columns

  26. 26

    How to add a time to given columns in a dataframe

  27. 27

    R - How to add sequential columns to dataframe on conditional?

  28. 28

    How to show all of columns name on pandas dataframe?

  29. 29

    how to split 'number' to separate columns in pandas DataFrame

HotTag

Archive