Python Forum
Python 3.11 data import question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python 3.11 data import question (/thread-39542.html)



Python 3.11 data import question - love0715 - Mar-05-2023

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


RE: Python 3.11 data import question - deanhystad - Mar-05-2023

Try pandas

https://pandas.pydata.org/


RE: Python 3.11 data import question - snippsat - Mar-05-2023

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.