Python 将numpy数组或列表附加到数据帧
大家好,我的问题与上述类似,但需要跨1k cols执行。 在解决上述链接的过程中,我遇到了一个意想不到的问题 假设我有一个数据帧df,如下所示:Python 将numpy数组或列表附加到数据帧,python,pandas,dataframe,Python,Pandas,Dataframe,大家好,我的问题与上述类似,但需要跨1k cols执行。 在解决上述链接的过程中,我遇到了一个意想不到的问题 假设我有一个数据帧df,如下所示: col1 col2 Col2 - col1000 0 1 3 1 2 4 2 3 7 3 4 5 col1 col2 Col2 - col1000 0 1 3 1 2 4 2 3 7 3 4 5 new extension
col1 col2 Col2 - col1000
0 1 3
1 2 4
2 3 7
3 4 5
col1 col2 Col2 - col1000
0 1 3
1 2 4
2 3 7
3 4 5
new extension of rows when appending
1 1 .
1 23 .
1 43 .
Nan . Nan
Nan . Nan
Nan . Nan
columns = [list_of_columns_from_dataframe_]
for i in columns:
mean = df[i].mean()
dev = df[i].std()
xx = np.random.normal(mu, sigma, 20)
xtra = {i: xx.tolist()}
df = df.append(pd.DataFrame(xtra))
在上面的几列中,我想用这个numpy数组扩展所有的列
xx = np.random.normal(mu, sigma, 413)
如何将其附加到输出如下的循环中:
col1 col2 Col2 - col1000
0 1 3
1 2 4
2 3 7
3 4 5
new extension of rows when appending
1 1 .
1 23 .
1 43 .
. . .
. . .
. . .
当在for循环中使用时,来自链接的解决方案为我提供了一个我不期望的数据帧:
first iteration
col1 col2 Col2 - col1000
0 1 3
1 2 4
2 3 7
3 4 5
new extension of rows when appending
1 Nan .
1 Nan .
1 Nan .
@ second Iteration for the second column I get this
col1 col2 Col2 - col1000
0 1 3
1 2 4
2 3 7
3 4 5
new extension of rows when appending
1 Nan .
1 Nan .
1 Nan .
Nan 1
Nan 23
Nan 43
但我所寻找的是这样的输出:
col1 col2 Col2 - col1000
0 1 3
1 2 4
2 3 7
3 4 5
col1 col2 Col2 - col1000
0 1 3
1 2 4
2 3 7
3 4 5
new extension of rows when appending
1 1 .
1 23 .
1 43 .
Nan . Nan
Nan . Nan
Nan . Nan
columns = [list_of_columns_from_dataframe_]
for i in columns:
mean = df[i].mean()
dev = df[i].std()
xx = np.random.normal(mu, sigma, 20)
xtra = {i: xx.tolist()}
df = df.append(pd.DataFrame(xtra))
我的代码如下所示:
col1 col2 Col2 - col1000
0 1 3
1 2 4
2 3 7
3 4 5
col1 col2 Col2 - col1000
0 1 3
1 2 4
2 3 7
3 4 5
new extension of rows when appending
1 1 .
1 23 .
1 43 .
Nan . Nan
Nan . Nan
Nan . Nan
columns = [list_of_columns_from_dataframe_]
for i in columns:
mean = df[i].mean()
dev = df[i].std()
xx = np.random.normal(mu, sigma, 20)
xtra = {i: xx.tolist()}
df = df.append(pd.DataFrame(xtra))