Question

Python with Pandas dataframe

I have a csv file that contains a large number of columns and rows. I need to write a script that concatenates some elements of the first row with some elements of the 2 row. Something like # if data[1][0] starts with ch then concatenate the element right below it. I have attached a picture of just a sample of my data. The booleans have to stay on there as is. But I must drop the empty columns. I'm just having difficulty doing this through using pandas dataframe. In order for me to run the debugger on my script I must run a main.py that is under my tools folder. Any help would be appreciated.

chla chla chla chla chla chla chla chla chla chla chla chla isma::cas_ism _cmd_mode_cmd_mode_cmd_modefcp::afcs_fafcp::pfcs_fa

I do not believe my dataframe is data. Here is my code so far.

import pandas as pd import numpy as np def ImportCustomScript(filename): data= pd.read_csv(filename, sep=., header=none) cs

Additional information: i need to join elements that say ch1a with the element right below it to read as part of the same column title. So the first row elements have to join together with the second row elements. please let me know if this still isn't clear

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

  • I have provided the properly commented code in both text and image format so you can easily copy the code as well as check for correct indentation.
  • I have provided the input/output image of the code so you can easily cross-check for the correct output of the code.
  • Have a nice and healthy day!!

CODE TEXT

import pandas as pd
import numpy as np

def ImportCustomScript(filename):
data=pd.read_csv(filename,sep='.',header=None)
# droping empty columns, using dropna method of pandas with axis=1
data=data.dropna(axis=1)
# concatenating first col and second column value if first value starts with ch
# using list comprehension to create list of concatenated values
# looping through each keys and fetch first and second value of col
# if first value start with ch concatenating first and second value
# otherwise returning first value
concatenated_values=[data[key][0]+data[key][1] if (data[key][0][:2] =='ch') else (data[key][0]) for key in data.keys()]
# replacing values of first column to concatenated_values
data.iloc[0] = concatenated_values
  
return data

ImportCustomScript('dataset.csv')

CODE IMAGE

4 7 1 import pandas as pd 2 import numpy as np 3 def ImportCustomScript(filename): 5 data=pd.read_csv(filename, sep=., head

INPUT IMAGE

D A B с 1 chla chla chla 2 isma::cas_ism _cmd_mode _cmd_model 3 single boolean uint32 4 11.42644024 0 0 5 11.42591953 0 o 6 1

OUTPUT IMAGE

o 1 2 4 Och1aisma: cas_ism ch1a_cmd_mode ch1a_cmd_mode1 ch1bisma: cas_isr isma cas_ism _cmd_mode _cmd_mode 1 isma cas_isr 2 s

Add a comment
Know the answer?
Add Answer to:
Python with Pandas dataframe I have a csv file that contains a large number of columns...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Pandas DataFrame in Python : I have csv file which has date column with object data...

    Pandas DataFrame in Python : I have csv file which has date column with object data type which ranges from 1908 to 2018: (Original) Date                 (My result) Date                  (I Need) Date                       17-Sep-08                                  2008-09-17                 1908-09-17 7-Sep-09                                    2009-09-07                  1909-09-07 .    (more years)                         .   (more years)               .     .                                                 .                                      . 8-Nov-07                                       2007-11-07                 2007-11-07 23-Sep-08                                     2008-09-23                 2008-09-23 29-Dec-18                                     2018-12-29                 2018-12-29 When I am converting it to datetime64[ns] or/and adding column as year after extracting just year values from date...

  • (a) Load the data file data/tips.csv into a pandas DataFrame called tips_df using the pandas read_table()...

    (a) Load the data file data/tips.csv into a pandas DataFrame called tips_df using the pandas read_table() function. Check the first five rows. (b) Create a new dataframe called tips by randomly sampling 6 records from the dataframe tips_df. Refer to the sample() function documentation. (c) Add a new column to tips called idx as a list ['one', 'two', 'three', 'four', 'five', 'six'] and then later assign it as the index of tips dataframe. Display the dataframe. (d) Create a new...

  • How to make a table from a csv file without using prettytable or pandas. I have...

    How to make a table from a csv file without using prettytable or pandas. I have to use format I'm trying to... I'm trying to get a column for "Type", "Total", and "Percent" from TipJoke.csv file. import csv with open('TipJoke.csv', newline='') as csv_file: filereader = csv.reader(csv_file, delimiter=' ') for row in filereader: print( '{:2} {:3} {:4}'.format('Type', 'Total', 'Percent')) print(', '.join(row))

  • Python Assignment In this assignment, you will use Pandas library to perform analysis on the dataset stored in the following csv file: breast-cancer-wisconsin.csv. Please write script(s) to do the fol...

    Python Assignment In this assignment, you will use Pandas library to perform analysis on the dataset stored in the following csv file: breast-cancer-wisconsin.csv. Please write script(s) to do the following: 1. Read the csv file and covert the dataset into a DataFrame object. 2. Persist the dataset into a SQL table and a JASON file. • Write the content of the DataFrame object into an SQLite database table. This will convert the dataset into a SQL table format. You can...

  • I have a dataframe with these columns: peo means people with cancer.I encode 'peo' collum to...

    I have a dataframe with these columns: peo means people with cancer.I encode 'peo' collum to low and hight and the have values 0 for low and 1 for hight. Egg , meat_other, mutton_goat, beed, pig , poultry are numeric values of the consumption of these types of foods. I would like to answer this question on my analysis: If this type of foods have a relation with cancer.All my variables are numeric , less country that is the names...

  • I have a dataframe with these columns: peo means people with cancer.I encode 'peo' collum to low ...

    I have a dataframe with these columns: peo means people with cancer.I encode 'peo' collum to low and hight and the have values 0 for low and 1 for hight. Egg , meat_other, mutton_goat, beed, pig , poultry are numeric values of the consumption of these types of foods. I would like to answer this question on my analysis: If this type of foods have a relation with cancer.All my variables are numeric , less country that is the names...

  • Lab Exercise #15 Assignment Overview This lab exercise provides practice with Pandas data analysis library. Data...

    Lab Exercise #15 Assignment Overview This lab exercise provides practice with Pandas data analysis library. Data Files We provide three comma-separated-value file, scores.csv , college_scorecard.csv, and mpg.csv. The first file is list of a few students and their exam grades. The second file includes data from 1996 through 2016 for all undergraduate degree-granting institutions of higher education. The data about the institution will help the students to make decision about the institution for their higher education such as student completion,...

  • Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int...

    Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int row) String field(int row, int column) Please use the CSVReader and CSVReaderTester class to complete the code. I have my own CSV files and cannot copy them to here. So if possible, just use a random CSV file. CSVReader.java import java.util.ArrayList; import java.util.Scanner; import java.io.*; /**    Class to read and process the contents of a standard CSV file */ public class CSVReader {...

  • Use the link in the Jupyter Notebook activity to access your Python script. Once you have...

    Use the link in the Jupyter Notebook activity to access your Python script. Once you have made your calculations, complete this discussion. The script will output answers to the questions given below. You must attach your Python script output as an HTML file and respond to the questions below. In this discussion, you will apply the statistical concepts and techniques covered in this week's reading about hypothesis testing for the difference between two population proportions. In the previous week’s discussion,...

  • 23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4...

    23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4 PROJECT 4: Using Pandas for data analysis and practice with error handling Overview In this project, you will use the Pandas module to analyze some data about some 20th century car models, country of origin, miles per gallon, model year, etc. Provided Input Files An input file with nearly 200 rows of data about automobiles. The input file has the following format (the same...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT