Python Forum
Python 3.11 data import question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3.11 data import question
#1
Hello , i am SAS programmer in data analytics world. new to python .

I just downloaded Python 3.11 on my personal Windows laptop

Question on importing csv and data manipulation.

Below is python code I copied from somewhere , that is supposed to import csv file. ( I think)
It seems to work but my question , if i want to drop or rename certain column, how do i do that?
For example, the Table contains a column named Location, if i want to remove or drop column ,how can I add my coding
in the following python code :

import csv


with open('sample.csv', newline='', encoding='utf-8') as f:
    reader = csv.reader(f)
        for row in reader:
            print(row)
Thank you !
TC
Yoriz write Mar-05-2023, 04:06 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Try pandas

https://pandas.pydata.org/
Reply
#3
Pandas is close to the way you work with data in SAS.
So Pandas(DataFrame) is in SAS(data set),
and the way work with data is similar.

The code you have is more standar Python,which also could also be nice to know,but it depend on your goals.
Can give an example of both.
import csv

with open('data.csv', newline='', encoding='utf-8') as f:
    reader = csv.reader(f)
    for row in reader:
        print(f'{row[0]}, {row[2]}')
Output:
Child, IsActive 11074165, NO 11094122, NO 11020499, NO 11018432, NO

import pandas as pd

df = pd.read_csv('data.csv')
Usage.
# The DataFrame
>>> df
      Child    Parent IsActive
0  11074165  11018432       NO
1  11094122  11033371       NO
2  11020499  11018277       NO
3  11018432  11020499       NO
>>> 
>>> df_new = df.drop('Parent', axis=1)
>>> df_new
      Child IsActive
0  11074165       NO
1  11094122       NO
2  11020499       NO
3  11018432       NO

>>> print(df_new.to_csv(index=False))
Child,IsActive
11074165,NO
11094122,NO
11020499,NO
11018432,NO

So in both have drop column Parent which could be your column Location.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Plot a pandas data fram via pyqtgraph with an modul import and qt designer widget Nietzsche 0 859 May-29-2023, 02:42 PM
Last Post: Nietzsche
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,914 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Can't import csv data JonWayn 4 1,407 Sep-18-2022, 02:07 AM
Last Post: JonWayn
  I try to import data from Excel table to Word Template. NewbiePyPy 0 3,291 Oct-21-2020, 12:25 PM
Last Post: NewbiePyPy
  Import CSV data into array and turn into integers DoctorSmiles 5 3,219 Jul-16-2020, 10:47 AM
Last Post: perfringo
  How to import data to excel jkessous 4 2,940 Jan-26-2020, 07:53 PM
Last Post: snippsat
  Early data import question VorpalPirate 2 2,262 Dec-09-2019, 10:52 PM
Last Post: snippsat
  How to import data from database to excel nick123 1 3,518 Oct-09-2019, 04:39 PM
Last Post: sm51251
  Import data from excel to Teradata steve87bg 3 4,773 Sep-18-2019, 01:36 PM
Last Post: steve87bg
  Import and/or read IMC measurement data Squipy 0 3,771 Aug-11-2019, 02:48 PM
Last Post: Squipy

Forum Jump:

User Panel Messages

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