Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View 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='')

View 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)

View file

@ -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)