While reading my text file, I initially skipped some rows. Now, I'm done modifying the column of interest "TMAX" using the code below. This exports but does not include the original lines that were skipped initially. How do I include them please? The text file must be in the original format for it to be useful for my next step.
Here are some codes I used
# Read a CSV file into a DataFrame
file_path = 'KSFN9130.WTH'
# Define the column names based on the file structure
column_names = ['DATE', 'SRAD', 'TMAX', 'TMIN', 'RAIN']
df = pd.read_csv(file_path, delim_whitespace=True, skiprows=5, names=column_names, na_values=['M', ' '])
# Change date column to pandas datetime object
df['DATE'] = pd.to_datetime(df['DATE'], format = '%Y%j')
# Create column containing only months
df['month'] = df['DATE'].dt.month
# Create column containing only weeks
df['week'] = df['DATE'].dt.isocalendar().week
# Select only summer m,months
summer_mask_idx = (df['month'] == 6) | (df['month'] == 7) | (df['month'] == 8)
# Group by year
for year, group in df[summer_mask_idx].groupby(df['DATE'].dt.year):
# Group each year by weeks
for week, week_group in group.groupby('week'):
# Identify the index of the maximum temperature values for each week
max_tmax_idx = week_group['TMAX'].idxmax()
#Locate the maximum values above in the original dataframe and increase them by 2
df.loc[max_tmax_idx, 'TMAX'] += 2
# Drop the WEEK and Month columns as they're no longer needed
df.drop(columns=['month','week'], inplace=True)
# Save the modified dataframe back to a text file
output_file_path = 'KSTX9130.WTH'
df.to_csv(output_file_path, sep=' ', index=False, header=False)
Read the first 5 lines of the file into a string. Then write them back to the output file before calling df.to_csv()
.
# Save the modified dataframe back to a text file
first_lines = ''
with open(file_path) as f:
for _ in range(5):
first_lines += f.readline()
output_file_path = 'KSTX9130.WTH'
with open(output_file_path, 'w') AS outf:
outf.write(first_lines)
df.to_csv(outf, sep=' ', index=False, header=False)