Python Forum
Pandas DataFrame visual
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas DataFrame visual
#1
import pandas as pd 
area = pd.Series({'California': 423967, 'Texas': 695662,
                  'New York': 141297, 'Florida': 170312,
                  'Illinois': 149995})
pop = pd.Series({'California': 38332521, 'Texas': 26448193,
                 'New York': 19651127, 'Florida': 19552860,
                 'Illinois': 12882135})
data = pd.DataFrame({'area':area, 'pop':pop})
print(data)
this is how it looks like:
Output:
area pop California 423967 38332521 Texas 695662 26448193 New York 141297 19651127 Florida 170312 19552860 Illinois 149995 12882135
instead of
Output:
area pop California 423967 38332521 Texas 695662 26448193 New York 141297 19651127 Florida 170312 19552860 Illinois 149995 12882135
Do you have any suggestion how to move area and pop to the right? It may sound banal but if I was doing this for a client it wouldn't look good.
Reply
#2
(Jul-04-2020, 10:12 PM)Truman Wrote: this is how it looks like:
No it dos not look like this when i run it.
import pandas as pd

area = pd.Series({'California': 423967, 'Texas': 695662,
                  'New York': 141297, 'Florida': 170312,
                  'Illinois': 149995})
pop = pd.Series({'California': 38332521, 'Texas': 26448193,
                 'New York': 19651127, 'Florida': 19552860,
                 'Illinois': 12882135})
data = pd.DataFrame({'area':area, 'pop':pop})

print(data)
Output:
area pop California 423967 38332521 Texas 695662 26448193 New York 141297 19651127 Florida 170312 19552860 Illinois 149995 12882135
So to make in the way it should possible be reset_index() and rename first column.
>>> data = data.reset_index()
>>> data
        index    area       pop
0  California  423967  38332521
1       Texas  695662  26448193
2    New York  141297  19651127
3     Florida  170312  19552860
4    Illinois  149995  12882135
>>> 
>>> data = data.rename(columns={'index': 'States'})
>>> data
       States    area       pop
0  California  423967  38332521
1       Texas  695662  26448193
2    New York  141297  19651127
3     Florida  170312  19552860
4    Illinois  149995  12882135  
Reply
#3
now I get
Output:
index area pop 0 California 423967 38332521 1 Texas 695662 26448193 2 New York 141297 19651127 3 Florida 170312 19552860 4 Illinois 149995 12882135
This didn't solve my issue because I don't want to see the ugly 'index' and columns are again not labeled well. On different machines and editors it looks different, I guess.
Reply
#4
(Jul-06-2020, 09:34 PM)Truman Wrote: This didn't solve my issue because I don't want to see the ugly 'index' and columns are again not labeled well
Because you have not done rename as i did?
(Jul-06-2020, 09:34 PM)Truman Wrote: On different machines and editors it looks different, I guess.
It should look the same if not using a strange editor Confused
Can do it new online environment like Colab,here a Notebook as see same result as i posted.
Reply
#5
After renaming there is again a problem with alignining
Output:
States area pop 0 California 423967 38332521 1 Texas 695662 26448193 2 New York 141297 19651127 3 Florida 170312 19552860 4 Illinois 149995 12882135
I use VS Code.
Reply
#6
(Jul-07-2020, 01:55 PM)Truman Wrote: I use VS Code.
There is no problem in VS Code for me,in output windows or Terminal window the output display is correct.
Only in run as Notebook inside Vs Code using # %%,is not display oaky on first print(),but after fix all is correct.
Here a couple of screenshot.
[Image: DaLc59.png]
Running NoteBook # %%
The first show it wrong,but there is not problem in code,see after fix the display is also correct.
[Image: UKtEPt.png]
I find it also more easy to work with JupyterLab Notebook in Browser where it belong,and not inside Vs Code.
Reply
#7
Unfortunately, I don't understand your last comment. Don't see a difference between the two images.
After adding print('-' * 50) columns are aligned. Not sure how this code exactly solved the issue.
Reply
#8
(Jul-08-2020, 08:38 PM)Truman Wrote: After adding print('-' * 50) columns are aligned. Not sure how this code exactly solved the issue
It's the way you combine Series to a DataFrame.
After States will have no index/column only area and pop.
>>> data.columns
Index(['area', 'pop'], dtype='object')
So to fix this use reset_index() and give a name.
>>> data = data.reset_index()
>>> data.columns
Index(['index', 'area', 'pop'], dtype='object')

# Rename to a useful name
data = data.rename(columns={'index': 'States'})

>>> data.columns
Index(['States', 'area', 'pop'], dtype='object')
>>> 
>>> data
       States    area       pop
0  California  423967  38332521
1       Texas  695662  26448193
2    New York  141297  19651127
3     Florida  170312  19552860
4    Illinois  149995  12882135 
There is no problem in doing this way to fix it.
I don't now how data is collect or that you have to combine two pd.Series like this.
There maybe is a way to make usable DataFrame for raw data,
but using reset_index() is a common way to fix thing to a usable DataFrame.
Reply
#9
Try using this code
import pandas as pd 
area = pd.Series({'California': 423967, 'Texas': 695662,
                  'New York': 141297, 'Florida': 170312,
                  'Illinois': 149995})
pop = pd.Series({'California': 38332521, 'Texas': 26448193,
                 'New York': 19651127, 'Florida': 19552860,
                 'Illinois': 12882135})
data = pd.DataFrame({'area':area, 'pop':pop},index=area.index)
data.index.name = 'states'

print(data)
Output:
area pop states California 423967 38332521 Texas 695662 26448193 New York 141297 19651127 Florida 170312 19552860 Illinois 149995 12882135
In case you are not familiar with the methods in the above code, go through this article of pandas
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML Decoder pandas dataframe column mbrown009 3 1,021 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,116 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,430 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,310 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,260 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,794 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,546 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,245 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 2,443 Jun-22-2021, 07:57 PM
Last Post: snippsat
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 2,961 Jan-11-2021, 09:53 PM
Last Post: Azureaus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020