簡體   English   中英

連接兩個數據框並將唯一值保存為 txt

[英]Concatenate two data-frames and save unique value as txt

這是我的第一篇文章,我正在尋求幫助。

我有 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)

在我遍歷組后,我如何將提取的值保存到 a.txt & 並將新的子目錄文件夾名稱設置為“extracted”?

謝謝

您是否嘗試從兩個數據框中提取唯一的距離和直徑值並將它們保存到 .txt 文件?

您可以使用.unique()方法從 dataframe 中的列中獲取唯一值:

unique_dist_vls = large.Distance.unique()
unique_diam_vls = medium.Diameter.unique()

然后您可以使用它來將每個數組保存到 a.txt 文件中(每個唯一值將打印在文件中的單獨行上):

import os
os.makedirs("extracted")

with open("./extracted/extracted.txt", "w") as txt_file:
   print("\n".join(unique_dist_vls), file=txt_file)

如果這不能完全解決您的問題,請說明您預期的 output 是什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM