python - 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:
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!
解决方案
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.
推荐阅读
- ruby-on-rails - Rails 从可用性中选择日期
- python-3.x - 类变量(不是实例变量)是具有相同类类型的对象的快捷方式?
- wordpress - 使用 wp_query 和通过 URL 发送的值为自定义帖子元正确配置 Wordpress 搜索过滤器
- javascript - 使用猫鼬添加不在模式中的字段
- microsoft-cognitive - Azure Cognitiveservices 计算机视觉 Java SDK 的源代码
- c# - 在运行时动态选择命名空间和类
- c# - 如何在实时图表中创建垂直线
- python - 在字符串中查找多个略有不同的子字符串
- nltk - 在 Python 中识别 Stanford Tregex 中的模式
- mysql - fail2ban 添加 PHP 函数