我正在迭代 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/