繁体   English   中英

如何在pandas python中使用.iloc将行写入.csv文件

[英]How to write rows to a .csv file using .iloc in pandas python

我正在使用正则表达式在 .csv 文件中搜索一些数字。 然后,当数字匹配时,这些行的索引使用 .iloc 写入 .csv,但出现错误。

for each in temp:

      if doLuhn(str(each)) is True:
          #print ("In the loop")
          creditcards.append(each)
          Validcardsfound = Validcardsfound + 1
          regex_match_index_list.append(i)

然后

for each in regex_match_index_list:
df.iloc[each].to_csv('test.csv')

注意:我创建了一个空的数据框 df。

但我收到以下错误

IndexError:单个位置索引器越界

尝试在 .iloc 之后输入列数而不是“each”。 假设您有 18 列,请尝试以下操作: df.iloc[:,17].to_csv('test.csv')

如果您想将根据某些要求(regex_match_index_list)创建的列表写入文件 test.csv:也许您可以尝试将其转换为一列的数据框并写入。

import pandas as pd
pd.DataFrame(regex_match_index_list, columns=['column_name_you_want']).to_csv('test.csv')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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