【问题标题】:Python pandas, multiple dataframes in onePython pandas,多个数据框合二为一
【发布时间】:2018-12-17 14:04:24
【问题描述】:

我有 100 个 CSV 文件。但是我需要一个数据框中所有数据框的一列的信息,以便我可以从中制作图表。当我追加一列时,总 Dataframe 保持为空。我做错了什么?

import os
import pandas as pd
import matplotlib.pyplot as plt

directory = os.fsencode('xx')
total_df = pd.DataFrame()
for file in os.listdir(directory):
    filename = os.fsdecode(file)
    if ('results' in filename):                                           
        temp_df = pd.read_csv(filename, sep=';')
        xCoor = temp_df.iloc[0,0]
        yCoor = temp_df.iloc[0,1]
        if (xCoor > 51 and xCoor < 52 and yCoor > 5 and yCoor < 6):
            data = temp_df['lon']
            total_df.append(data)
print(total_df[:])

这是输出:

Empty DataFrame
Columns: []
Index: []

已解决:

使用 total_df = total_df.append(..) 为我工作

【问题讨论】:

  • 查看最里面的 if 语句中的比较运算符
  • DataFrame.append 返回一个 new DataFrame,在这种情况下它没有分配给任何东西。
  • 使用total_df = total_df.append(data) 而不仅仅是total_df.append(data)

标签: python pandas


【解决方案1】:

我没有详细说明,但您的 if 语句将始终返回 False

if (xCoor > 51 and xCoor < 52 and yCoor > 5 and yCoor < 6):

您必须在某处添加&gt;=&lt;=

【讨论】:

  • xcoor=51.5, ycoor = 5.5 ?
猜你喜欢
  • 1970-01-01
  • 2018-11-01
  • 2015-04-02
  • 2020-11-02
  • 2018-03-09
  • 2021-10-09
  • 2021-12-16
  • 1970-01-01
  • 2021-06-25
相关资源
最近更新 更多