'Can't iterate through excel or .csv with pandas or openpyxl
I'm trying to iterate through a column of a pandas dataframe from .csv with pandas, and I've tried .xlsx with openpyxl, and copy to a new dataframe if the string contains substring "Leading Edge". I'm getting the error "TypeError: argument of type 'float' is not iterable" even though the columns and rows should be integers. I've tried printing out sample values and it works fine, so I'm not sure why it isnt working with either a for lop with int i as iterator, nor .iter_rows iterator (openpyxl). Any help is greatly appreciated!
Using openpyxl
import pandas as pd
from openpyxl import Workbook
from openpyxl import load_workbook
path = r"C:\Users\austi\Downloads\IVY 19 Clock Genes"
workbook = load_workbook(path + "\\newSheetfromJupyter.xlsx", read_only=True)
readSheet = workbook.active
wb = Workbook()
newSheet = wb.active
df = pd.DataFrame
for row in readSheet.iter_rows(min_row = 2,
min_col = 1,
max_col = 18,
values_only = True):
if("Leading Edge" in row[1]): #ERROR CORRESPONDS TO HERE
df.append((cell.value for cell in row[0:18]))
print(df)
Using pandas
LE = dfWithCol.groupby(dfWithCol.tumor_region)
for i in range(len(dfWithCol.index)):
if("Leading Edge" in dfWithCol.tumor_region[i]): #ERROR HERE
LEf = LE.get_group(dfWithCol.tumor_region[i])
print(LEf)
Solution 1:[1]
You can't iterate an int, you can only iterate sequence types (such as list, str, and tuple) and some non-sequence types (like dict, file objects, and objects of any classes you define with an iter() method or with a getitem() method). I just added a str() to your code and it worked on my device.
import pandas as pd
from openpyxl import Workbook
from openpyxl import load_workbook
workbook = load_workbook("test.xlsx")
readSheet = workbook.active
wb = Workbook()
newSheet = wb.active
df = pd.DataFrame
for row in readSheet.iter_rows(min_row = 2,
min_col = 1,
max_col = 18,
values_only = True):
if("Leading Edge" in **str(row[1])**): #ERROR CORRESPONDS TO HERE
df.append((cell.value for cell in row[0:18]))
print(df)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tuurtje11 |
