[英]append for loop output in single dataframe
I'm trying to append the for loop output in pandas dataframe as:我正在尝试将 Pandas 数据帧中的 for 循环输出附加为:
import pandas as pd
for i in range(1,5):
p=i+4
df = pd.DataFrame({'filename':i,'comments':p},index=[0])
print (df)
It returns as multiple dataframe as:它作为多个数据帧返回:
filename comments
0 1 5
filename comments
0 2 6
filename comments
0 3 7
filename comments
0 4 8
Output expected as:预期输出:
filename comments
1 1 5
2 2 6
3 3 7
4 4 8
I'm looking to build and fill a dataframe from a loop only我正在寻找仅从循环中构建和填充数据框
You can append a list as a series to a dataframe like so您可以像这样将列表作为系列附加到数据框
import pandas as pd
df = pd.DataFrame(columns=['filename','comments'])
for i in range(1,5):
df = df.append(pd.Series([i, i+4], index=df.columns), ignore_index=True)
print (df)
you must define the empty dataframe to begin with and then from within the loop append the list as a new row您必须先定义空数据框,然后从循环中将列表附加为新行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.