I have CSV file:
Veids;Razotajs;Modelis;Spec;Cena
HDD;Seagate;Barracuda;1TB;35,99
HDD;Seagate;Barracuda;2TB;79
HDD;Intenso;;5TB;140
HDD;Fujitsu;;1TB;71,4
HDD;Western Digital;Purple Series;2TB;59
SSD;Western Digital;Blue;500GB;57
SSD;Samsung;970 EVO Plus;500GB;90,99
RAM;Corsair;Vengeance LPX;16GB;99
RAM;Corsair;ValueSelect;4G;27
RAM;Kingston;HyperX Fury Black;8GB;46
RAM;Patriot;Viper Steel;64GB;355
GPU;Gigabyte;GeForce GT 710;2GB;79
GPU;Asus;TUF Radeon RX 6900 XT;16GB;2625
GPU;Asus;GeForce GTX 1650;4GB;349
GPU;Palit;GeForce GT 710;2GB;52
CPU;AMD;Ryzen 5 3600;3.6GHz;239
CPU;AMD;Ryzen 7 5800X;3.8GHz;469
CPU;Intel;i7-10700K;3.8GHz;327
CPU;Intel;i9-11900;2.5GHz;455
MB;Gigabyte;B450 AORUS Elite V2;;96,93
MB;MSI;Z490-A PRO;;165
MB;Asus;TUF GAMING B460-PLUS;;113,99
MB;ASRock;B460M Pro4;;100
PSU;Be Quiet!;System Power 9;500W;54
PSU;Chieftec;ATX 2.3;600W;45,98
PSU;Corsair;TX-M Series;750W;110,46
PSU;Corsair;ATX 2.4;650W;89
Korpuss;Corsair;iCUE 220T RGB Black;;109
Korpuss;Aerocool;Cylon Mini RGB Black;;56
Korpuss;Aerocool;Zauron Mid-Tower ATX Black;;55
Korpuss;Chieftec;Mini Tower IX-01B-OP;;32
Disc;Asus;ZenDrive U9M External DVD Writer Silver;;36,3
Disc;Gembird;External USB CD/DVD Drive;;18,1
PSU;Corsair;ATX 2.4;650W;89
asd;sadad;asffa;2355hgh;423
and I need function to delete rows by it full data I tried this
import csv
import pandas
RAM=[]
ram=values[1] #input from gui
with open('komponentes.csv') as f:
csvf = csv.reader(f, delimiter=';')
for row in csvf:
row_string = ';'.join(row)
df=pd.read_csv("komponentes.csv")
df.set_value(0,ram,"")
df.to_csv("komponentes.csv", index=False)
but it gives me just
AttributeError: 'DataFrame' object has no attribute 'set_value'. Did you mean: '_set_value'
I also tried to do it through CSV, but as a result I only got new lines between the old ones. Aswell as i tried
Report_Card = pd.read_csv("komponentes.csv")
Report_Card.drop(Report_Card.index[(Report_Card["Veids;Razotajs;Modelis;Spec;Cena"] == ram)],axis=0,inplace=True)
which gave no result at all
so as my value can be every ram row, I need function which detects which one is the right and deletes it, for example I have in
ram=values[1]
this row
RAM;Kingston;HyperX Fury Black;8GB;46
so it should be deleted and csv file should become like this
Veids;Razotajs;Modelis;Spec;Cena
HDD;Seagate;Barracuda;1TB;35,99
HDD;Seagate;Barracuda;2TB;79
HDD;Intenso;;5TB;140
HDD;Fujitsu;;1TB;71,4
HDD;Western Digital;Purple Series;2TB;59
SSD;Western Digital;Blue;500GB;57
SSD;Samsung;970 EVO Plus;500GB;90,99
RAM;Corsair;Vengeance LPX;16GB;99
RAM;Corsair;ValueSelect;4G;27
RAM;Patriot;Viper Steel;64GB;355
GPU;Gigabyte;GeForce GT 710;2GB;79
GPU;Asus;TUF Radeon RX 6900 XT;16GB;2625
GPU;Asus;GeForce GTX 1650;4GB;349
GPU;Palit;GeForce GT 710;2GB;52
CPU;AMD;Ryzen 5 3600;3.6GHz;239
CPU;AMD;Ryzen 7 5800X;3.8GHz;469
CPU;Intel;i7-10700K;3.8GHz;327
CPU;Intel;i9-11900;2.5GHz;455
MB;Gigabyte;B450 AORUS Elite V2;;96,93
MB;MSI;Z490-A PRO;;165
MB;Asus;TUF GAMING B460-PLUS;;113,99
MB;ASRock;B460M Pro4;;100
PSU;Be Quiet!;System Power 9;500W;54
PSU;Chieftec;ATX 2.3;600W;45,98
PSU;Corsair;TX-M Series;750W;110,46
PSU;Corsair;ATX 2.4;650W;89
Korpuss;Corsair;iCUE 220T RGB Black;;109
Korpuss;Aerocool;Cylon Mini RGB Black;;56
Korpuss;Aerocool;Zauron Mid-Tower ATX Black;;55
Korpuss;Chieftec;Mini Tower IX-01B-OP;;32
Disc;Asus;ZenDrive U9M External DVD Writer Silver;;36,3
Disc;Gembird;External USB CD/DVD Drive;;18,1
PSU;Corsair;ATX 2.4;650W;89
asd;sadad;asffa;2355hgh;423
just without this data
CodePudding user response:
Solution1:
To remove some specific rows, you may consider apply()
of the data frames useful.
def is_specific_veids(x):
if x == 'RAM':
return False
else:
return True
#Use which ever conditions you want.
df = pd.read_csv("file.csv")
newdf = df[df["Vields"].apply(is_specific_vields) == True] #In this way we can find only rows which we want.
newdf.to_csv("new_file.csv")
Solution2
newdf = df[df["Vields"]!='RAM' & df["Cena"] == 24] #You can use other conditions using & and || (or) to get the specific dataframe.
newdf.to_csv("new_file.csv")
Solution 3
Use groupby
method to get specific groups and remove the group which you want and concatenate others.
groupdfs = []
for group, groupdf in df.groupby('Vields'): #You can use more columns by passing a list of column names.
if group != 'RAM': #Write conditions accordingly
groupdfs.append(groupdf)
newdf = pd.concat(groupdfs)
newdf.to_csv("new_file.csv")
You can use any of above but the solution2 is more easy and concrete. Just write the conditions which data you want and you will get that.
Hope that this helps you.
CodePudding user response:
You can use the csv module to delete a row in a list of rows, or drop a row between a reader and a writer.
We'll see two examples, both of which use the csv module, and have a predefined row to delete/drop:
import csv
suppress_row = ["RAM", "Kingston", "HyperX Fury Black", "8GB", "46"]
Delete row from list of rows
If you want to read in the CSV data, delete the row, then further manipulate the data, I suggest turning the reader into a list of rows and using the list's remove() method:
with open("input.csv", newline="") as f_in:
reader = csv.reader(f_in, delimiter=";")
all_rows = list(reader)
# just to prove to ourselves it is there to begin with
assert suppress_row in all_rows
all_rows.remove(suppress_row)
# and now prove it's gone
assert suppress_row not in all_rows
# do more stuff with all_rows...
Drop a row between reader and writer
If you just want to remove the row and save the data without it, you can open a reader and writer at the same time, passing a row from the reader immediately to the writer, unless the row needs to be deleted/skipped; "selectively streaming" the rows from input to output:
with open("input.csv", newline="") as f_in, open("output.csv", "w", newline="") as f_out:
writer = csv.writer(f_out, delimiter=";")
reader = csv.reader(f_in, delimiter=";")
for row in reader:
if row == suppress_row:
continue
writer.writerow(row)