Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 选择CSV文件中具有相同标题的特定列,并输出到新的CSV文件_Python_Pandas_Csv - Fatal编程技术网

Python 选择CSV文件中具有相同标题的特定列,并输出到新的CSV文件

Python 选择CSV文件中具有相同标题的特定列,并输出到新的CSV文件,python,pandas,csv,Python,Pandas,Csv,尽可能简单地解释它。我在一个目录中有多个csv文件。这些csv文件具有不同高度的温度读数(以及其他变量)。我想要一个脚本,通过所有的csv文件,输出每个温度列和日期到它自己的单独列一个新的csv称为说。。“主温度” 假设我有5个文件,输出的csv文件头应该是这样的: 日期,温度,日期,温度,日期,温度,温度,日期,温度,日期,温度 我想你可能想要这样的东西 import os import pandas as pd dfmaster = pd.DataFrame(columns=["date"

尽可能简单地解释它。我在一个目录中有多个csv文件。这些csv文件具有不同高度的温度读数(以及其他变量)。我想要一个脚本,通过所有的csv文件,输出每个温度列和日期到它自己的单独列一个新的csv称为说。。“主温度”

假设我有5个文件,输出的csv文件头应该是这样的:

日期,温度,日期,温度,日期,温度,温度,日期,温度,日期,温度


我想你可能想要这样的东西

import os
import pandas as pd

dfmaster = pd.DataFrame(columns=["date", "temperature"])
source = "path_to_dir_containing_csv_files"

for filename in os.listdir(source):
    fullpath = os.path.join(source, filename)
    if os.path.isfile(fullpath) and fullpath.endswith(".csv"):
        dfchild = pd.read_csv(fullpath)
        dfmaster = pd.concat([dfmaster, dfchild])

print(dfmaster.reset_index(drop=True))
dfmaster.to_csv("master.csv", index=False) #---> save the csv file

这回答了你的问题吗?如果您希望以后避免麻烦和维护噩梦,请不要在列名中使用重复项……到底是什么问题?你试过什么,做过什么研究吗?堆栈溢出不是免费的代码编写服务。见:。