擅长:python、mysql、java
<p>因此,您可以轻松检查您的值。我已经用下面的源代码试过了。
我首先创建了两个数据帧(您已经有了),其中包含一个值从0到399的列表:</p>
<pre><code> import pandas
df1 = pandas.DataFrame({"col1": list(range(400))})
df2 = df1
</code></pre>
<p>然后用搜索索引初始化一个列表,并迭代该列表</p>
<pre><code> search_idx = [23, 4, 310]
</code></pre>
<p>您的dataframe对象具有列。每列都有一个名称。在我的示例中:我只有一列,该列的名称为“col1”。列名是dataframe对象的一个字段,因此可以通过点操作访问它</p>
<pre><code> #Open the file
with open('merge.txt', 'a') as file:
#Iterate over all indexes
for idx in search_idx:
#Write Values to list
values = [idx, df1.col1[idx], df2.col1[idx]]
#convert to string
values = map(str, values)
#Concatenate to one string and write to file
text = " ".join(values)
file.write(text + '\n')
</code></pre>
<p>这将创建一个包含以下数据的文件merge.txt:</p>
<pre><code> 23 23 23
4 4 4
310 310 310
</code></pre>
<p>如果您想在数据之间添加一个“,”,请在迭代中更改“连接”行</p>
<p>致意</p>