对两个有多行多列的csv文件中所有具有相同行名列名的内容提取出来并输出成新的文件
时间: 2024-06-11 18:09:17 浏览: 231
这个任务可以通过Python中的pandas库来实现。具体步骤如下:
1. 导入pandas库
```python
import pandas as pd
```
2. 使用pandas的read_csv()函数读取两个csv文件,并将第一列设置为行名
```python
df1 = pd.read_csv('file1.csv', index_col=0)
df2 = pd.read_csv('file2.csv', index_col=0)
```
3. 使用pandas的merge()函数将两个DataFrame合并,并只保留相同的行和列
```python
merged_df = pd.merge(df1, df2, left_index=True, right_index=True)
```
4. 将合并后的DataFrame保存为新的csv文件
```python
merged_df.to_csv('new_file.csv')
```
完整代码如下:
```python
import pandas as pd
df1 = pd.read_csv('file1.csv', index_col=0)
df2 = pd.read_csv('file2.csv', index_col=0)
merged_df = pd.merge(df1, df2, left_index=True, right_index=True)
merged_df.to_csv('new_file.csv')
```
相关问题
提取一个csv文件中所有等于G20的数据的行名列名并组成新的表格文件
这是一个Python代码示例,使用Pandas库读取CSV文件并提取所有等于G20的数据行和列:
```python
import pandas as pd
# 读取CSV文件
df = pd.read_csv('data.csv', index_col=0)
# 创建一个新的DataFrame,用于存储等于G20的数据行和列
g20_df = pd.DataFrame()
# 遍历所有行和列,找到等于G20的数据
for row in df.index:
for col in df.columns:
if df.loc[row, col] == 'G20':
# 将行名和列名添加到新的DataFrame中
g20_df.loc[row, col] = df.loc[row, col]
# 将新的DataFrame保存为CSV文件
g20_df.to_csv('g20_data.csv')
```
这个代码假设CSV文件中第一列是行名,第一行是列名。如果CSV文件的格式不同,需要适当修改代码。
把一个文件所有内容为G20的行名列名整合成字典形式,在另外一个文件中以所有键对值匹配结果并输出
假设文件名为file1.txt,文件内容如下:
```
,USA,China,Japan,Germany,UK,France,India,Italy,Brazil,Canada
USA,G20,G20,G20,G20,G20,G20,G20,G20,G20,G20
China,G20,G20,G20,G20,G20,G20,G20,G20,G20,G20
Japan,G20,G20,G20,G20,G20,G20,G20,G20,G20,G20
Germany,G20,G20,G20,G20,G20,G20,G20,G20,G20,G20
UK,G20,G20,G20,G20,G20,G20,G20,G20,G20,G20
France,G20,G20,G20,G20,G20,G20,G20,G20,G20,G20
India,G20,G20,G20,G20,G20,G20,G20,G20,G20,G20
Italy,G20,G20,G20,G20,G20,G20,G20,G20,G20,G20
Brazil,G20,G20,G20,G20,G20,G20,G20,G20,G20,G20
Canada,G20,G20,G20,G20,G20,G20,G20,G20,G20,G20
```
假设要处理的文件名为file2.txt,文件内容如下:
```
USA
Japan
India
Brazil
```
以下是Python代码实现:
```python
# 读取文件1,将内容转换为字典形式
with open('file1.txt', 'r') as f1:
content = f1.read().splitlines()
headers = content[0].split(',')
data = content[1:]
g20_dict = {}
for row in data:
row_data = row.split(',')
country = row_data[0]
for idx, val in enumerate(row_data[1:]):
if val == 'G20':
g20_dict[(country, headers[idx+1])] = val
# 读取文件2,以键值对匹配结果并输出
with open('file2.txt', 'r') as f2:
for line in f2:
country = line.strip()
for header in headers[1:]:
if (country, header) in g20_dict:
print(f"{country}, {header}: {g20_dict[(country, header)]}")
```
输出结果:
```
USA, China: G20
USA, Japan: G20
USA, Germany: G20
USA, UK: G20
USA, France: G20
USA, India: G20
USA, Italy: G20
USA, Brazil: G20
USA, Canada: G20
Japan, USA: G20
Japan, China: G20
Japan, Germany: G20
Japan, UK: G20
Japan, France: G20
Japan, India: G20
Japan, Italy: G20
Japan, Brazil: G20
Japan, Canada: G20
India, USA: G20
India, China: G20
India, Japan: G20
India, Germany: G20
India, UK: G20
India, France: G20
India, India: G20
India, Italy: G20
India, Brazil: G20
India, Canada: G20
Brazil, USA: G20
Brazil, China: G20
Brazil, Japan: G20
Brazil, Germany: G20
Brazil, UK: G20
Brazil, France: G20
Brazil, India: G20
Brazil, Italy: G20
Brazil, Brazil: G20
Brazil, Canada: G20
```
阅读全文