繁体   English   中英

如何在 for 循环中向 Pandas Dataframe 添加字符串值?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM