I am being given a dataframe that contains column ('nutrition facts'), and was asked to iterate over each value in a cell, pull out each value in the column of Nutrition Facts and append it to new columns. For example, 51.5,173.4,269.8,368.1 and 352.9 (first value of each row in column "Nutrition Facts") will be added into a new column named "column One" accordingly. And the question is asking me to use a loop to do so. I'm having trouble creating this loop. Any help would be appreciated.
Asked
Active
Viewed 87 times
0

Ransaka Ravihara
- 1,786
- 1
- 13
- 30

NIA
- 41
- 4
-
I have no idea what you need , but the duplicated question should address your problem – BENY Jun 16 '20 at 01:53
-
If you can't answer it, please don't close my question as I need someone else's help. The question was clear to USE A LOOP to do it, not to_list. But the page you provided me doesn't solve my problem! Thank you! – NIA Jun 16 '20 at 02:00
-
I leave it open , good luck , if you mention for loop then , that is almost not a panda question – BENY Jun 16 '20 at 02:02
-
1Instead of sharing an image of some data, share a part of the DataFrame as code. What have you tried yourself, because the problem is fairly trivial? What part of the problem are you having trouble with? Looping over the values? Adding columns to a DataFrame? Adding the new data for each row? The overall size of the data set? – Grismar Jun 16 '20 at 02:09
-
I have updated the picture.. sorry for the confusion – NIA Jun 16 '20 at 02:16
-
@YOBEN_S Hey I figured out the problem, thank you so much for your help!! – NIA Jun 16 '20 at 06:23
2 Answers
0
Instead of iterating all of the values in the shell, you can use pandas apply method. as an example :
import pandas as pd
import numpy as np
def function(val):
return val*val
df = pd.DataFrame([[4, 9]] * 3, columns=['A', 'B'])
df['C']=df['A'].apply(function)
print(df)
This will give output like this.
A B C
0 4 9 16
1 4 9 16
2 4 9 16
First, You have to define your own function which relevant to your desire. In here, my example I take all the values in column 'A'
and append another column named 'C'
with a square value of column 'A'
.
EDIT
In your case below code will work,
def function(data):
return data[0]
df['column_One']=df['Nutrition Facts'].apply(function)

Community
- 1
- 1

Ransaka Ravihara
- 1,786
- 1
- 13
- 30
-
Thanks for the help! The issue was that the whole cell was a string and the data type was object, I wasn't able to separate them and call the value by their index. But I figured it out finally!! Thank you!! – NIA Jun 16 '20 at 06:22
0
The simple solution:
Expand using pd.Series()
then merge:
df_temp = df["Nutrition Facts"].apply(pd.Series)
df.merge(df_temp, left_index=True)
Using a for loop:
Iterate over values and add to an array of dictionaries:
expanded_data = []
for row in df["Nutrition Facts"].values:
row_data = {}
for i, value in enumerate(row):
row_data[i] = value
# Save to all data
expanded_data.append(row_data)
# Now convert to a dataframe and merge
df_temp = pd.DataFrame.from_records(expanded_data)
df.merge(df_temp, left_index=True)

Yaakov Bressler
- 9,056
- 2
- 45
- 69
-
Thank you so much! I have tried the pd.Series() solution but it didn't work, because the whole cell was a string so I wasn't able to call the value inside the string. I have tried doing it with the loop you provided, but I ended up with " Out[180]: [{0: '[', 1: '5', 2: '1', 3: '.', 4: '5', 5: ',', 6: ' ', 7: '0', 8: '.', 9: '0', 10: ',',...)}) – NIA Jun 16 '20 at 06:04
-
I finally figured out the problem. I was able to figure it out by separating the string and change the type of the column; thank you so much for your help! – NIA Jun 16 '20 at 06:14