Posts: 404
Threads: 94
Joined: Dec 2017
Jul-04-2020, 10:12 PM
(This post was last modified: Jul-04-2020, 10:12 PM by Truman.)
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.
Posts: 7,320
Threads: 123
Joined: Sep 2016
(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
Posts: 404
Threads: 94
Joined: Dec 2017
Jul-06-2020, 09:34 PM
(This post was last modified: Jul-06-2020, 09:35 PM by Truman.)
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.
Posts: 7,320
Threads: 123
Joined: Sep 2016
(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
Can do it new online environment like Colab,here a Notebook as see same result as i posted.
Posts: 404
Threads: 94
Joined: Dec 2017
Jul-07-2020, 01:55 PM
(This post was last modified: Jul-07-2020, 01:56 PM by Truman.)
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.
Posts: 7,320
Threads: 123
Joined: Sep 2016
Jul-07-2020, 05:11 PM
(This post was last modified: Jul-07-2020, 05:11 PM by snippsat.)
(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]](https://imagizer.imageshack.com/v2/xq90/924/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]](https://imagizer.imageshack.com/v2/xq90/922/UKtEPt.png)
I find it also more easy to work with JupyterLab Notebook in Browser where it belong,and not inside Vs Code.
Posts: 404
Threads: 94
Joined: Dec 2017
Jul-08-2020, 08:38 PM
(This post was last modified: Jul-08-2020, 08:38 PM by Truman.)
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.
Posts: 7,320
Threads: 123
Joined: Sep 2016
Jul-09-2020, 08:43 AM
(This post was last modified: Jul-09-2020, 08:43 AM by snippsat.)
(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.
Posts: 43
Threads: 0
Joined: Apr 2020
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
|