Iterate in dataframe with more that one element per entry to have lists
I have a dataframe that looks kind of like this. It has two columns and some columns have more than one data. For instance the first row in the first column has "tom-15", "to-13" and the second column has 10, 3.3. data = [['"tom-15", "to-13"', 10, 3.3], ['"nick-12"', 15.5], [, 14]] df = pd.DataFrame(data, columns=['Att1', 'Att2']) I need to use these columns as parameters for other parts of my code but as some columns have two or more entries I want to create a list on each case. I tried this: for index, row in df.iterrows(): l1 = row["Att1"].tolist() l2 = row["Att2"].tolist() It gives me an error 'str' object has no attribute 'tolist'. How do I create a list in each case? I want the first lists to look like this l1=["tom-15", "to-13"] and the first l2 like this l2=[10, 3.3]. Therefore, the last l1 is l1=[] and the last l2 l2=[14]
I have a dataframe that looks kind of like this. It has two columns and some columns have more than one data. For instance the first row in the first column has "tom-15", "to-13" and the second column has 10, 3.3.
data = [['"tom-15", "to-13"', 10, 3.3], ['"nick-12"', 15.5], [, 14]]
df = pd.DataFrame(data, columns=['Att1', 'Att2'])
I need to use these columns as parameters for other parts of my code but as some columns have two or more entries I want to create a list on each case. I tried this:
for index, row in df.iterrows():
l1 = row["Att1"].tolist()
l2 = row["Att2"].tolist()
It gives me an error 'str' object has no attribute 'tolist'
. How do I create a list in each case?
I want the first lists to look like this l1=["tom-15", "to-13"]
and the first l2 like this l2=[10, 3.3]
. Therefore, the last l1 is l1=[]
and the last l2 l2=[14]