• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Create new DataFrame based on existing DataFrame

Resolved: Create new DataFrame based on existing DataFrame

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I want to reorganize some data in a Pandas DataFrame from an existing DataFrame such that I can have a subset of row values (in the minimal example below ‘City’ as columns, and remove rows not having either of the chosen values, below ‘New York’ and ‘Amsterdam’).
To illustrate what I mean by the above, I have a DataFrame that looks like this:
I would like to create a DataFrame that looks like this:
Current code:
Here is my code so far:
How do I go from a DataFrame that looks like the first one to one that looks like the latter?

Answer:

As order is important we first need to create a categorical column based on City.
Then we can create a pivot using pd.pivot_table
Finally, your difference column

If you have better answer, please add a comment about this, thank you!

dataframe numpy pandas python
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Vaadin 14.9 – Redirect on Session Destroy Event

26/03/2023

Resolved: How to separate multiple answers in one column, for multiple columns, by creating extra columns

26/03/2023

Resolved: How to set consistent decimal separators in R data frame?

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.