简体   繁体   English

如何将列从一个 csv 文件复制到另一个 csv 文件?

[英]How to copy columns from one csv file to another csv file?

I have input.csv file with below format我有以下格式的 input.csv 文件

number day
5      Mon
4      Tue
3      Wed
2      Thu

and I want to copy this data to another output.csv file with reverse order我想将此数据以相反的顺序复制到另一个 output.csv 文件

number day
2      Thu
3      Wed
4      Tue
5      Mon

I have written this code but don't know how to proceed with reverse thing我已经编写了这段代码,但不知道如何进行反向操作

cols = ["number", "day"]
file_path = "input.csv"
pd.read_csv(file_path, usecols=cols).to_csv("output.csv", index=False)
  • t.csv t.csv
number,day
5,Mon
4,Tue
3,Wed
2,Thu
  • script脚本

with open('t.csv','r') as file:
    f=csv.reader(file)
    data=[]
    for row in f:
        data.append(row)
    header=data[0]
    rows=data[1:]
    rows.sort(reverse=False)

with open('t_out.csv','w',newline='') as file_out:
    f=csv.writer(file_out)
    f.writerow(header)
    f.writerows(rows)


  • t_out.csv t_out.csv
number,day
2,Thu
3,Wed
4,Tue
5,Mon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将列从一个 CSV 文件复制到另一个 CSV 文件? - How do I copy columns from one CSV file to another CSV file? 将一行从一个 csv 复制到另一个 csv 文件 - Copy a single row from one csv to another csv file Python 3,将特定列从一个csv文件复制到另一个csv - Python 3, Copy specific column from one csv file to another csv 如何使用 csv 阅读器到 map 列从一个文件到另一个 - How to use csv reader to map columns from one file to another 如何将一个CSV文件中的多行和一列复制到另一个CSV Excel中? - How to copy multiple rows and one column from one CSV file to another CSV Excel? 如何使用 Python 将行从一个 CSV 复制到另一个 CSV 文件 - How to copy rows from one CSV to another CSV file using Python 从csv文件中提取特定的列,然后使用python将其复制到另一个 - to extract specific columns from a csv file and copy it to another using python 如何从一个csv文件追加到另一个? - How to append from one csv file to another? 如何使用python将.csv文件中的一行复制到另一行 - How to copy one row in a .csv file to another row with python 如何使用 Python 和 Z251D1BBFE9A3B678CEAZ30DC 将 csv 文件中一个单元格的值复制到另一个 csv 文件? - How can I copy the value of one cell in a csv file to another csv file using Python and Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM