【问题标题】:Saving extracted column to a txt file in ascending order按升序将提取的列保存到 txt 文件
【发布时间】:2021-07-14 02:50:30
【问题描述】:

我需要一些帮助以升序将列中的值写入文本文件。

我当前的代码创建了一个名为values 的目录,并将从列中提取的值保存到 .txt 文件中,但它不是我想要的升序。

values_dir=os.path.join(cwd, 'values')

if not os.path.exists(values_dir):
    os.mkdir(values_dir)
    
with open(os.path.join(values_dir, 'values.txt'), "w") as txt_file:
    for name, group in split_location:
        txt_file.write(str(name) + '\n')

代码将我的值保存为

data23
data17
data88

我希望它保存为

data17
data23
data88

如果有人能指出我正确的方向,将不胜感激,谢谢。

编辑

我通过字段 DataData_Unit 中的唯一值拆分了 2 个大型数据帧

datafile = pd.read_csv('location.csv')

datafile_large = pd.read_csv('large.csv')

split_location = datafile.groupby('Data')
split_large = datafile_large.groupby('Data_Unit')

然后,我遍历这些组并根据其唯一值将拆分的数据帧保存到子目录,同时保留父文件名。

for name, group in split_location:
    sub_dir = os.path.join(cwd, name)
    if not os.path.exists(sub_dir):
        os.mkdir(sub_dir)
    group = group.drop(['Data'], axis=1)
    group.to_csv(sub_dir + "/location.csv", index=0)

for name, group in split_large:
    sub_dir = os.path.join(cwd, name)
    if not os.path.exists(sub_dir):
        os.mkdir(sub_dir)
    group = group.drop(['Data_Unit'], axis=1)
    group.to_csv(sub_dir + "/large.csv", index=0)

最后我创建values.txt 文件,如开头所述。但是希望将.txt文件中保存的值按升序排列。

values_dir=os.path.join(cwd, 'values')

if not os.path.exists(values_dir):
    os.mkdir(values_dir)
    
with open(os.path.join(values_dir, 'values.txt'), "w") as txt_file:
    for name, group in split_location:
        txt_file.write(str(name) + '\n')

【问题讨论】:

    标签: python pandas sorting text


    【解决方案1】:

    试试这个:

        names, groups = map(list, zip(*split_location))
        names.sort()
        
        for name in names:
            txt_file.write(str(name) + '\n')
    

    代替:

        for name, group in split_location:
            txt_file.write(str(name) + '\n')
    

    【讨论】:

    • 非常感谢您的回答 :) 不幸的是,这些值仍然没有按升序保存,我无法理解它
    • split_location 长什么样子?
    • 我添加了一些编辑,以便更广泛地解释我想要实现的目标。再次感谢@Mady Daby
    • @Kam-ALIEN 将原来的 for 循环更改为 for name, group in split_location.sort_values('Data'): 是否有效?
    • 我之前确实尝试过这种方法,但出现了 AttributeError: 'DataFrameGroupBy' object has no attribute 'sort_values'...感谢您的回复
    【解决方案2】:

    你可以使用python内置的sorted函数或者列表的sort方法。另一个答案显示了排序方法,所以我在这里使用排序。

    对于 python 3,也可以使用 pathlib

    from pathlib import Path
    
    values_dir = Path.home() / 'values'
    values_dir.mkdir(exist_ok=True)
    
    # step one: get a list of names
    # from your example, split_location looks like
    # an iterable of two-item tuple or list
    names = sorted([str(item[0]) for item in split_location])
    
    # step two: write the list of sorted names
    # you can write just one string by joining your
    # list of names with newline characters
    newf = values_dir / 'values.txt'
    newf.write_text('\n'.join(names))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-12
      • 2015-07-20
      • 2012-04-16
      相关资源
      最近更新 更多