Python newbie here, I am not able to create a function which can extract certain columns' values into another form. I have tried to run a loop multiple times to get the data, but I am not able to find a good pythonic way to do it. Any help or suggestion would be welcome.
PS: The column with "Loaded with" is has the information that what items are loaded into it, but you can also get this info by seeing that there are few columns with name item_1L...
I was not able to find a better way to input the data on SO, so I have created a csv file of the dataframe.
I need the LBH of the separate items in the form of
Item1=4.6x4.3x4.3 Item2=4.6x4.3x4.3 or any other easily iterable way.
EDIT: When I say I needed the answer in the form of 4.6x4.3x4.3, I really meant I needed it in the form of "4.6x4.3x4.3" i.e.not the product of the numbers. I need the string format like this :
import pandas as pd
df = pd.DataFrame({'0': ['index', 'Name', 'Loaded
with','item_0L','item_0B','item_0H','item_1L','item_1B','item_1H'],
'1': [0, 'Tata-
417','01','4.3','4.3','4.6','4.3','4.3','4.6',]})
string format
index Loadedwith item_0L item_0B item_0H item_1L item_1B item_1H
1 01 4.6 4.3 4.3 4.6 4.3 4.3'
Here is what I have been trying:
def get_df (df):
total_trucks = len(df)
total_items = 0
for i in range(len(df["Loaded with"])):
total_items += len((df["Loaded with"].iloc[i]))
for i in range(len(df["Loaded with"])):
for j in range(total_items):
for k in range(len((df["Loaded with"].iloc[i]))):
# pass
# print("value of i j k is {} {} {}".format(i,j,k))
if(pd.isnull(Packed_trucks.loc["item_" + str(j) + "L"])):
display(Packed_trucks["item_" + str(j) + "L"])
# return 0
get_df(Packed_trucks)