![](/img/trans.png)
[英]How to add values to a Pandas Dataframe using a nested for loop?
[英]How to add string values to Pandas Dataframe within a for loop?
我有一个df_causation
数据df_causation
,我将它创建为一个带有相应列名的空数据df_causation
。
df_causation = pd.DataFrame(columns=['Question'])
我有一个for
循环,其中对于循环的每次迭代,我都会得到一个名为cause_str
的新字符串,如下所示:-
for i in range(len(X_test)):
cause_str = hyp.join(f_imp) #cause_str is a new string obtained for each iteration
(忽略获取方式,我只是举了个例子)
我想将这些新字符串(cause_str)
(所有这些)附加到我的 Pandas 数据帧df_causation
的问题列中的每个连续行。 这样做有什么合适的方法吗?
编辑:预期输出
df_causation. **Causation**
Row 0 cause_str from i = 0 th iteration in loop
Row 1 cause_str from i = 1 th iteration in loop etc.
IIUC 正确,这应该工作:
dfd['**Causation**'] = df['df_causation.'].apply(lambda x: f'cause str from i = {x.split(" ")[1]} th iteration in loop')
df3
df_causation. **Causation**
0 Row 0 cause str from i = 0 th iteration in loop
1 Row 1 cause str from i = 1 th iteration in loop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.