在数据框中迭代并在文本上插入

时间:2019-07-04 02:01:02

标签: arrays python-3.x pandas

在熊猫中加载了一个大小为m*n的DataFrame,与值{2}到列20的列n相比,m可能很大。 m*n中的每个值都必须添加到特定文本中,这意味着任何值之间的文本都是恒定的。

我尝试使用ForIf嵌套语句,但如何使从df.iloc [0,0]迈向df.iloc [m,n]并插入文本效果不佳。

textA + df.iloc[0,0] + textB + df.iloc[0,1] + .... + textX + df.iloc[0,n]
textA + df.iloc[1,0] + textB + df.iloc[1,1] + .... + textX + df.iloc[1,n]
.
.
.
textA + df.iloc[m,0] + textB + df.iloc[m,1] + .... + textX + df.iloc[m,n]

我有2个文件,一个包含textA textB ... textX,第二个文件是csv类型,其中生成了pandas数据帧。

上面有数据框和文本准备数组。

感谢小费。

1 个答案:

答案 0 :(得分:0)

不清楚您的要求,但请看下面的代码是否能够帮助您在从文本文件添加信息时在df.iloc [0,0]到df.iloc [m,n]之间进行迭代(在这种情况下是文本变量)

# i am using a dummy variable for the text data as i am not sure how your data look like
text = ['textA', 'textB', 'textC']
new_text = []

# using a nested for loop to iterate
for row in range(len(df)): 
    for col in range(len(df.columns)):
        new_text.append(text[col] + str(df.iloc[row, col]))