Question

In: Computer Science

How can I use Python without Pandas to create a new.csv file with the headers "c1,...

How can I use Python without Pandas to create a new.csv file with the headers "c1, c2, c3, c4, c5, c6" and the first line of data "8, 5, -9, 7, 2.1, 1.7" from the original.csv file?

Solutions

Expert Solution

Answer

Here is your answer,if you have any doubt please comment, i am here to help you,

Here is the code ,

import csv   #import csv module
import numpy as np
with open('original.csv', newline='') as f: #reading original csv file and getting fist line.
    reader = csv.reader(f)
    row1 = next(reader)
# data rows of csv file  
row1=np.reshape(row1, (1, 6)) #change based on your column, first line
    
# field names  
fields = ['c1', 'c2', 'c3', 'c4', 'c5', 'c6']  
    
# name of csv file  
filename = "new.csv"
    
# writing to csv file  
with open(filename, 'w') as csvfile:  
    # creating a csv writer object  
    csvw = csv.writer(csvfile)  
    # writing the fields  
    csvw.writerow(fields)  
     # writing the data rows  
    csvw.writerows(row1) 

output

Any doubt please comment

Thanks in advance


Related Solutions

I have an excel file imported into canopy (python) using import pandas as pd. The excel...
I have an excel file imported into canopy (python) using import pandas as pd. The excel file has headers titled: datetime created_at PM25 temperatureF dewpointF    humidityPCNT windMPH    wind_speedMPH wind_gustsMPH pressureIN precipIN these column headers all have thousands of data numbers under them. How could i find the average of all of the numbers in each column and plot them on 1 graph (line graph or scatter plot) Thank you.(please comment out your code)
Using Python coding language (with or without Pandas and/or NumPy), 1. Can you define function sleep...
Using Python coding language (with or without Pandas and/or NumPy), 1. Can you define function sleep to tell whether the participant are of the ages through 18 to 60 and sleep less than 6 hours per day? 2. Develop codes to check whether the sleep function you defined can make correct judgement. Make sure you write comments or create informative vairable name so that I can understand how you check the sleep function. (Hints: You can create toy data/dataframe to...
How would I create a nested dictionary given a csv file in Python? Say I want...
How would I create a nested dictionary given a csv file in Python? Say I want to make a dictionary that read {'country':{'China':'Fit', 'China':'Overweight', 'USA': 'Overweight', 'USA': 'Fit', 'England':'Fit'...}, 'category':{'Asian':'Fit', 'Caucasian': 'Overweight', 'Caucasian':'Overweight', 'Asian': 'Fit', 'Middle Eastern': 'Fit'...}} given a file that had country category Weight China Asian Fit China Caucasian Overweight USA Caucasian Overweight USA Asian Fit England Middle Eastern Fit... ... And so on in the file.
Pandas exercises: 1. Write a python program using Pandas to create and display a one-dimensional array-like...
Pandas exercises: 1. Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. 2. Write a python program using Pandas to convert a Panda module Series to Python list and print it's type. (Hint: use ds.tolist() to convert the pandas series ds to a list) 3. Create a pandas dataframe called **my_df** with index as integer numbers between 0 to 10, first column (titled "rnd_int") as 10 integer random numbers between...
Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure...
Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure and then print a. the first 3 rows and the last 3 of the dataset b. the 3 cars with the lowest average-mileage c. the 3 cars with the highest average-mileage. Solve using PYTHON PROGRAMMING
How can I do this in python ??? In order to create the Exam Result objects...
How can I do this in python ??? In order to create the Exam Result objects correctly, one must find the student object and the subject object that the exam result should refer to. The distributed code contains O (n) methods that perform sequential searches to find them in the lists. Create new implementations of oving7. find _student and oving7.find_ topic that is more effective. oving7. find _student: def find _student (student no, student list):      for student in student list:...
How can I write a Python program that runs without crashing even though it has an...
How can I write a Python program that runs without crashing even though it has an error that a compiler would have caught and how can I write a Java program that crashes even after the compiler checked it. Please add comments that explain the problem, the outcome, and what this proves in both of the programs.
In bash how can I read an input file and create variables for specific lines and...
In bash how can I read an input file and create variables for specific lines and columns from the input file? For example I have Q W E R T Y U I A S D F So if I wanted to make a variable where Variable = U, how would I create it.
Can you please solve these questions/ statements using python? I started with "importing" the file. I...
Can you please solve these questions/ statements using python? I started with "importing" the file. I only need question one to be answered not two-four. Can use whatever data frame of choice, I just need a sample code for each line. Thank you #1. #Fit a linear regression model on data: USA_housing.csv to predict the Price of the house. import pandas as pd housing_df = pd.read_csv("USA_Housing.csv") #Income: Avg. area income #Age: Avg age of the houses #Bedrooms: Avg No of...
How to calculate Traffic LOS '' Level of service '' without using FFS. I can use...
How to calculate Traffic LOS '' Level of service '' without using FFS. I can use collected volume, and how can I do that ? and what are the information's needed ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT