与
list.append
,
pandas.DataFrame.append
是
不
就地操作。稍微修改一下代码就可以了:
import pandas as pd
df = pd.DataFrame()
def my_func():
global df
d = pd.DataFrame()
for i in range(10):
dct = {
"col1": i,
"col2": 'value {}'.format(i)}
d = d.append(dct, ignore_index=True) # <<< Assignment needed
# df.append(dct, ignore_index=True) # Does not seem to append anything to the global variable
df = d # does not assign any values to the global variable
my_func()
df.head()
输出:
col1 col2
0 0.0 value 0
1 1.0 value 1
2 2.0 value 2
3 3.0 value 3
4 4.0 value 4