Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
11
Task/CSV-data-manipulation/Python/csv-data-manipulation-1.py
Normal file
11
Task/CSV-data-manipulation/Python/csv-data-manipulation-1.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import fileinput
|
||||
|
||||
changerow, changecolumn, changevalue = 2, 4, '"Spam"'
|
||||
|
||||
with fileinput.input('csv_data_manipulation.csv', inplace=True) as f:
|
||||
for line in f:
|
||||
if fileinput.filelineno() == changerow:
|
||||
fields = line.rstrip().split(',')
|
||||
fields[changecolumn-1] = changevalue
|
||||
line = ','.join(fields) + '\n'
|
||||
print(line, end='')
|
||||
22
Task/CSV-data-manipulation/Python/csv-data-manipulation-2.py
Normal file
22
Task/CSV-data-manipulation/Python/csv-data-manipulation-2.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import csv
|
||||
from pathlib import Path
|
||||
from tempfile import NamedTemporaryFile
|
||||
|
||||
filepath = Path('data.csv')
|
||||
temp_file = NamedTemporaryFile('w',
|
||||
newline='',
|
||||
delete=False)
|
||||
|
||||
with filepath.open() as csv_file, temp_file:
|
||||
reader = csv.reader(csv_file)
|
||||
writer = csv.writer(temp_file)
|
||||
|
||||
header = next(reader)
|
||||
writer.writerow(header + ['SUM'])
|
||||
|
||||
for row in reader:
|
||||
row_sum = sum(map(int, row))
|
||||
writer.writerow(row + [row_sum])
|
||||
|
||||
temp_file_path = Path(temp_file.name)
|
||||
temp_file_path.replace(filepath)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import pandas as pd
|
||||
|
||||
filepath = 'data.csv'
|
||||
|
||||
df = pd.read_csv(filepath)
|
||||
rows_sums = df.sum(axis=1)
|
||||
df['SUM'] = rows_sums
|
||||
df.to_csv(filepath, index=False)
|
||||
Loading…
Add table
Add a link
Reference in a new issue