【发布时间】:2021-06-26 02:56:59
【问题描述】:
这是我第一次发帖寻求帮助。
我有 2 个共享一些唯一值的大型 csv 文件,我编写了一个小型 python 脚本来帮助提取唯一字段并将它们保存到子目录中。我遇到的问题是我想将提取的值作为提取的.txt 文件保存到父文件夹。
import numpy as np
import pandas as pd
import os
import json
large = pd.read_csv('large.csv')
medium = pd.read_csv('medium.csv')
#Grouped and split our dataframes by 'Distance & Diameter'
split1_groups = large.groupby('Distance')
split2_groups = medium.groupby('Diameter')
#loop through the groups and save to directories based on unique values
for name, group in split1_groups:
if not os.path.exists(name):
os.mkdir(name)
group.to_csv(name + "/large.csv", index=0)
for name, group in split2_groups:
if not os.path.exists(name):
os.mkdir(name)
group.to_csv(name + "/medium.csv", index=0)
在遍历组后,我如何将提取的值保存到 .txt 中,并将新的子目录文件夹名称设置为“extracted”?
谢谢
【问题讨论】:
标签: python pandas dataframe concatenation