【问题标题】:Collecting data in loop using Pythons Pandas Dataframe使用 Python Pandas Dataframe 循环收集数据
【发布时间】:2015-11-09 12:04:22
【问题描述】:

我正在尝试使用 python 的 pandas 模块从 csv 文件中提取数据。实验数据有 6 列(比如说 a、b、c、d、e、f),我有一个模型目录列表。并非每个模型都有所有 6 个“物种”(列),所以我需要专门为每个模型拆分数据。这是我的代码:

    def read_experimental_data(self,experiment_path):
        [path,fle]=os.path.split(experiment_path)
        os.chdir(path)
        data_df=pandas.read_csv(experiment_path) 
#        print data_df
        experiment_species=data_df.keys() #(a,b,c,d,e,f)
#        print experiment_species
        for i in self.all_models_dirs: #iterate through a list of model directories.
            [path,fle]=os.path.split(i)
            model_specific_data=pandas.DataFrame()
            species_dct=self.get_model_species(i+'.xml') #gives all the species (culuns) in this particular model
#            print species_dct
            #gives me only species that are included in model dir i
            for l in species_dct.keys(): 
                for m in experiment_species:
                    if l == m:
                         #how do i collate these pandas series into a single dataframe?
                        print data_df[m]

上面的代码为我提供了正确的数据,但我无法以可用的格式收集它。我试图合并和连接它们,但没有快乐。有没有人知道如何做到这一点?

谢谢

【问题讨论】:

    标签: python loops pandas


    【解决方案1】:

    您可以从 data_df 创建一个新的 DataFrame,方法是向它传递您想要的列列表,

    import pandas as pd
    df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [7,8,9]})
    df_filtered = df[['a', 'c']]
    

    或使用您的一些变量名称的示例,

    import pandas as pd
    data_df = pd.DataFrame({'a': [1,2], 'b': [3,4], 'c': [5,6],
                       'd': [7,8], 'e': [9,10], 'f': [11,12]})
    experiment_species = data_df.keys()
    species_dct = ['b', 'd', 'e', 'x', 'y', 'z']
    good_columns = list(set(experiment_species).intersection(species_dct))
    df_filtered = data_df[good_columns]
    

    【讨论】:

      猜你喜欢
      • 2018-10-13
      • 2013-12-22
      • 2019-04-13
      • 2019-05-18
      • 2016-09-21
      • 1970-01-01
      • 2018-07-09
      • 2021-12-28
      • 2021-10-02
      相关资源
      最近更新 更多