This tutorial shows how to use Python Pandas to merge two TXT files containing weather data and site coordinates. The first TXT file contains date, site, and three values, while the second file contains site, latitude, and longitude. The goal is to combine these files to create a new file with date, site, latitude, longitude, and the three values.

import pandas as pd

# Read the first TXT file
df1 = pd.read_csv('file1.txt', delimiter='\t', names=['date', 'site', 'value1', 'value2', 'value3'])

# Read the second TXT file
df2 = pd.read_csv('file2.txt', delimiter='\t', names=['site', 'latitude', 'longitude'])

# Merge the two DataFrames based on site
df = pd.merge(df1, df2, on='site')

# Convert the date column to datetime
df['date'] = pd.to_datetime(df['date'])

# Group by date, site, latitude, and longitude, then calculate the mean for each group
df = df.groupby(['date', 'site', 'latitude', 'longitude']).mean().reset_index()

# Save the results to a new file
df.to_csv('output.txt', sep='\t', index=False)

This code assumes the following:

  • The date format in the first TXT file is yyyy-mm-dd.
  • The latitude and longitude columns in the second TXT file are named 'latitude' and 'longitude'.

If these assumptions don't hold, you need to modify the code accordingly.

This example demonstrates a common data analysis task using Pandas: merging data from multiple sources to create a comprehensive dataset. It highlights the power of Pandas for data manipulation and analysis.

Python Pandas: Merge Two TXT Files for Geographic Data Analysis

原文地址: https://www.cveoy.top/t/topic/jXYe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录