python pandas 没有连接到空 DataFrame 中

标签 python pandas ipython

我正在迭代 DataFrame,评估每一行,然后使用 concat() 方法将其粘贴到另一个 DataFrame 中。然而,接收的DataFrame仍然是空的。

import pandas as pd

empty = DataFrame(columns=('col1', 'col2'))

d = {'col1' : Series([1, 2, 3]),
    'col2' : Series([3, 4, 5])
}

some_data = DataFrame(d)

print empty
print some_data
print 'concat should happen below'

for index, row in some_data.iterrows():
    pd.concat([empty, DataFrame(row)])

print empty # should contain 3 rows of data

输出:

Empty DataFrame
Columns: [col1, col2]
Index: []
   col1  col2
0     1     3
1     2     4
2     3     5
concat should happen below
Empty DataFrame
Columns: [col1, col2]
Index: []

最佳答案

如果您希望它存储值,则需要更新 empty:empty = pd.concat([empty, DataFrame(row)]) 您也可以连接整个DataFrame,试试这个print pd.concat([empty,some_data])

如果您想过滤行,可以尝试以下操作:

def f(r):
    #check the row here
    return True #returns True or False if include/exclude the row

print some_data.groupby(some_data.index).filter(f)

关于python pandas 没有连接到空 DataFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21196572/

相关文章:

python - 如何检查用户是否在不同应用程序的 mediawiki 中登录?

python - 如何在 pandas 数据框中堆叠 wthin 来执行其引用?

python - 当所有值为正时面积图的绘制比例

python - 如何将我的数据框拆分为不同的数据框?

python - Jupyter Notebook 内存管理

Python Spyder 显示符号数学

python - 如何从 PyPi 包中提取依赖项

python - 将纪元(即 01/01/0001 午夜)转换为 pandas 中的 DateTime

python jupyter ipynb vscode

python - 如何在交互式 session 中重新加载 Django 模型而不丢失我的本地人?