#๐Ÿ”’ How can I use Pandas to convert columns in a CSV file to rows for the same index element?

7 messages ยท Page 1 of 1 (latest)

shy ermine
#

Hi there! I have a CSV file with sample data and a lot of columns. I need to extract some data from this file. The new file should have only two columns (Project ID and Project Name) from the original file. In addition to that, I need to create three more columns (Bedroom Type, Stat, and Stat Value). The rows under these columns should be the column names.

For example, I have the columns Project ID, Project Name, ....., 0 Bedroom sizes from, 0 Bedroom sizes to, ......, 3.5 Bedroom prices from, 3.5 Bedroom prices to. I don't need any data from the columns in between. The values in the bedroom columns are numerical.

I need the new CSV file to fill rows with data such as:
1, Condo, 0 Bedroom, sizes from, 482
1, Condo, 0 Bedroom, sizes to, 580
......
1, Condo, 3.5 Bedroom, prices from, 1482
1, Condo, 3.5 Bedroom, prices to, 1580

How can I achieve this? There are 50 unique projects in the file. I need all of them, but the new CSV file should have them like above. What is the best method?

atomic shardBOT
#

@shy ermine

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wooden epoch
wooden epoch
#

if you drop the CSV in this chat, that would help.

atomic shardBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.