首页 > 解决方案 > Process list with Dataframe Typ

问题描述

I create a list and fill it with data from text files:

indir = "..."
infiles = [os.path.join(indir, f) for f in os.listdir(indir) if f.endswith('.txt')]

dataframes = []
for f in infiles:
    dataframes.append(pd.read_csv(f,sep='\t',skiprows=21,header=None))

and it looks like that:

dataframes - List

Now I would like to process the data in the DataFrame matrix. E.g. to interpolate or maybe delete a column.

How could I implement this? Thanks a lot for the answers!

标签: pythonpandaslistdataframe

解决方案


Lets consider what your code does: you are appending objects of type pandas.DataFrame to a python list dataframes, therefore if you want to work with the dataframes inside the list you should iterate through it. For example if you want to work with a selection of the first 10 columns of every DataFrame you could use the following code:

selection = range(10)
for df in dataframes:
   smaller_df = df[selection]
   # Do whatever you want with smaller_df

I suggest you read the pandas guide for indexing and selecting data as working with dataframes can be a bit tricky.


推荐阅读