Python Forum
deleting columns in CSV file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
deleting columns in CSV file
#5
i guess it's okay, but still,
here's the code:

import pandas as pd

data = pd.read_csv('/home/tal/investing/allstocks.csv')


print("Original 'allstocks.csv' CSV Data: \n")
print(data)

df = pd.DataFrame(data)

df.drop(df.columns[3:], axis=1, inplace=True)
df.to_csv('/home/tal/investing/allstocks.csv')


print("\nCSV Data after deleting the column 'year':\n")
print(df)
and attached is a screenshot of the result,
as you can see there's an added column at the beginning (the most left) of digits (signifying the row number),
isn't the row number already taken into account ? (or is it just in Excel where it gives the rows their numbers ?)
so if a column is added to signify the row's numbers (like an index) i have no problem, but suppose i delete an additional time a column that was not deleted in the first deletion, what happens is it adds another column (the rows index twice),

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
deleting columns in CSV file - by astral_travel - Nov-25-2022, 09:53 PM
RE: deleting columns in CSV file - by snippsat - Nov-25-2022, 10:37 PM
RE: deleting columns in CSV file - by astral_travel - Nov-26-2022, 12:31 PM
RE: deleting columns in CSV file - by deanhystad - Nov-26-2022, 01:33 PM
RE: deleting columns in CSV file - by astral_travel - Nov-26-2022, 02:26 PM
RE: deleting columns in CSV file - by snippsat - Nov-26-2022, 02:35 PM
RE: deleting columns in CSV file - by astral_travel - Nov-26-2022, 05:23 PM
RE: deleting columns in CSV file - by snippsat - Nov-26-2022, 07:14 PM
RE: deleting columns in CSV file - by astral_travel - Nov-26-2022, 09:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create Choices from .ods file columns cspower 3 809 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  Create csv file with 4 columns for process mining thomaskissas33 3 920 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  Converting a json file to a dataframe with rows and columns eyavuz21 13 5,341 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  Replace columns indexes reading a XSLX file Larry1888 2 1,100 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  rows from sql query need to write to a file as columns sjcsvatt 6 2,599 Oct-09-2021, 12:45 AM
Last Post: snippsat
  [Solved] Using readlines to read data file and sum columns Laplace12 4 3,767 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  Python Matplotlib: Create chart for every 4 columns in Excel file JaneTan 2 2,909 Feb-28-2021, 05:02 AM
Last Post: JaneTan
  How to fill parameter with data from multiple columns CSV file greenpine 1 1,757 Dec-21-2020, 06:50 PM
Last Post: Larz60+
  Read strings and numbers in columns from a file suvadip 4 3,103 Aug-11-2020, 09:37 PM
Last Post: suvadip
  Rearrange Columns in a CSV File rjj920 1 5,772 May-09-2020, 03:26 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020