How to get values from dataframe with dynamic columnsHow to make good reproducible pandas examplesHow to get the current time in PythonHow do I sort a dictionary by value?How to sort a dataframe by multiple column(s)Selecting multiple columns in a pandas dataframeAdding new column to existing DataFrame in Python pandasHow to change the order of DataFrame columns?Delete column from pandas DataFrame by column nameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers
Can the Produce Flame cantrip be used to grapple, or as an unarmed strike, in the right circumstances?
Copycat chess is back
Is there a familial term for apples and pears?
Is there any use for defining additional entity types in a SOQL FROM clause?
What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?
What happens when a metallic dragon and a chromatic dragon mate?
Need help identifying/translating a plaque in Tangier, Morocco
"My colleague's body is amazing"
A poker game description that does not feel gimmicky
Are white and non-white police officers equally likely to kill black suspects?
Extreme, but not acceptable situation and I can't start the work tomorrow morning
What does "enim et" mean?
Manga about a female worker who got dragged into another world together with this high school girl and she was just told she's not needed anymore
Information to fellow intern about hiring?
How to answer pointed "are you quitting" questioning when I don't want them to suspect
LWC and complex parameters
Landing in very high winds
Does bootstrapped regression allow for inference?
Re-submission of rejected manuscript without informing co-authors
Is there a way to make member function NOT callable from constructor?
Is domain driven design an anti-SQL pattern?
What is the meaning of "of trouble" in the following sentence?
Why do UK politicians seemingly ignore opinion polls on Brexit?
Can I find out the caloric content of bread by dehydrating it?
How to get values from dataframe with dynamic columns
How to make good reproducible pandas examplesHow to get the current time in PythonHow do I sort a dictionary by value?How to sort a dataframe by multiple column(s)Selecting multiple columns in a pandas dataframeAdding new column to existing DataFrame in Python pandasHow to change the order of DataFrame columns?Delete column from pandas DataFrame by column nameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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)
python pandas dataframe
add a comment |
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)
python pandas dataframe
Hi Rohit, would would mind creating an example like this Thanks
– anky_91
Mar 7 at 17:53
1
@anky_91 Sure, and thanks for telling me that.
– Rohit Kumar
Mar 7 at 17:54
add a comment |
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)
python pandas dataframe
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)
python pandas dataframe
python pandas dataframe
edited Mar 8 at 7:22
Rohit Kumar
asked Mar 7 at 17:51
Rohit KumarRohit Kumar
174219
174219
Hi Rohit, would would mind creating an example like this Thanks
– anky_91
Mar 7 at 17:53
1
@anky_91 Sure, and thanks for telling me that.
– Rohit Kumar
Mar 7 at 17:54
add a comment |
Hi Rohit, would would mind creating an example like this Thanks
– anky_91
Mar 7 at 17:53
1
@anky_91 Sure, and thanks for telling me that.
– Rohit Kumar
Mar 7 at 17:54
Hi Rohit, would would mind creating an example like this Thanks
– anky_91
Mar 7 at 17:53
Hi Rohit, would would mind creating an example like this Thanks
– anky_91
Mar 7 at 17:53
1
1
@anky_91 Sure, and thanks for telling me that.
– Rohit Kumar
Mar 7 at 17:54
@anky_91 Sure, and thanks for telling me that.
– Rohit Kumar
Mar 7 at 17:54
add a comment |
3 Answers
3
active
oldest
votes
May be something like:
m=df.loc[:,df.filter(like='item').columns]
df['Item1']=m.filter(like='0').astype(float).prod(axis=1)
df['Item2']=m.filter(like='1').astype(float).prod(axis=1)
Output:
index Loadedwith item_0L item_0B item_0H item_1L item_1B item_1H Item1 Item2
1 1 4.6 4.3 4.3 4.6 4.3 4.3 85.054 85.054
EDIT
df['Item1']=m.astype(str).filter(like='0').apply(lambda x: 'X'.join(x),axis=1)
df['Item2']=m.astype(str).filter(like='1').apply(lambda x: 'X'.join(x),axis=1)
print(df)
index Loadedwith item_0L item_0B item_0H item_1L item_1B item_1H
0 1 1 4.6 4.3 4.3 4.6 4.3 4.3
Item1 Item2
0 4.6X4.3X4.3 4.6X4.3X4.3
I am sorry, but I did not want the float product of the values, but the string values in a form like 4.6x4.3x4.3 with the letter x in between. I have edited the question, apologies for the time wasted because of unclear question.
– Rohit Kumar
Mar 8 at 7:42
1
@RohitKumar how about the section under EDIT?
– anky_91
Mar 8 at 7:58
Thanks for the reply, is there any way we can make the part where you saydf['Item2']
in a dynamic way like df[Item + str(i)] or something ?
– Rohit Kumar
Mar 8 at 10:57
1
i think you can but again, since the filters are changing, for loops may be involved. if possible for you, you can try posting a fresh question for this. :)
– anky_91
Mar 8 at 10:59
1
@RohitKumar unforunately cannot access the link, i use a corporate laptop mat be that's why
– anky_91
Mar 8 at 11:06
|
show 2 more comments
I'm a bit confused so I apologize if this is general, but it seems like you either need to parse the data or iterate through it. I'd recommend something along the lines of these:
Parse Line
f = open(file, "r")
line = f.readline()
data = []
while len(line) != 0:
data.append(line.strip(","))
//other code and stuff
line = f.readline()
f.close()
This will open a file and will read the data and form a list of lists based off of the data. In doing this it becomes very easy to iterate through the list, making a segway into iterating.
Iterating
Should you need to iterate through a list of you values, a for loop is the easiest way. If you need to quickly get the entire row or column, though, I'd recommend
data = [your data]
row = data[0][:]
column = data[:][0]
just replace the 0 with whatever index you need. NOTE: This will only work with two-dimensional lists, which is why I recommend parsing as I previously showed.
Edit: You can find more examples with this by looking into list comprehension and list splicing
Hm...thanks for the new direction, though can you guide me through where I am might be going wrong with mine?
– Rohit Kumar
Mar 7 at 18:09
@RohitKumar there are a few optimizations I'd make. First, trypandas.read_csv
first to get your data and then take a look atpandas.loc[]
next. Pandas Doc
– Gabe Ron
Mar 7 at 18:20
add a comment |
This solution will leverage pd.melt
function and create a table where each line would be a combination of a Truck (Index) and an Item Number
df = pd.read_csv('df.csv')
# We will operate on a subset of columns, leaving just index and columns we need
truck_level_df = df.drop(['Name', 'TruckID', 'Length', 'Breadth',
'Height', 'Volume', 'Weight', 'Price', 'Quantity', 'Loaded with'],
axis = 1)
truck_level_df:
index item_0L item_0B item_0H item_1L item_1B item_1H
0 1 4.6 4.3 4.3 4.6 4.3 4.3
# Create table with all the items and their measures
item_measure_level_df = truck_level_df.melt(id_vars = 'index',
var_name = 'item_id_and_measure', value_name = 'item_val')
# Remove unneeded substring
item_measure_level_df['item_id_and_measure'] =
item_measure_level_df['item_id_and_measure'].str.replace('item_', '')
# Extract Item ID
item_measure_level_df['item_id'] =
item_measure_level_df['item_id_and_measure']
.str.replace(r'[A-Z]*', '', case = False)
# Create df where each line is a combination
# of a Truck and an item
item_level_df = item_measure_level_df[['index', 'item_id']].drop_duplicates()
item_level_df:
index item_id_and_measure item_val item_id
0 1 0L 4.6 0
1 1 0B 4.3 0
2 1 0H 4.3 0
3 1 1L 4.6 1
4 1 1B 4.3 1
5 1 1H 4.3 1
Final step:
item_measure_level_df['item_val'] = item_measure_level_df['item_val'].astype('str')
# Group by Item and get LxHxB string
item_level_df['volume_string'] = item_measure_level_df.sort_values(by = ['index','item_id_and_measure']).groupby(['index','item_id'])['item_val'].apply(lambda x: ' x '.join(x)).values
The output:
index item_id volume_string
0 1 0 4.3 x 4.3 x 4.6
3 1 1 4.3 x 4.3 x 4.6
This solution will digest as many groups of columns as you would have
Shared notebook: https://colab.research.google.com/drive/16xUCMCH7rhOOp9Jwlv2RISnnmpzK-06d#scrollTo=lRDVe6B40VsH
I can't seem to run your code, there is an error replace() got an unexpected keyword argument 'regex' . Please use this colab to try your own code.
– Rohit Kumar
Mar 8 at 7:39
colab is using pandas 0.22.0 for some reason, regex was introduced in 0.23
– Dennis Lyubyvy
Mar 8 at 14:54
This is working (I have removed regex parameter and created a regex pattern instead): colab.research.google.com/drive/…
– Dennis Lyubyvy
Mar 8 at 14:55
Also I have posted solution that gives you a string like this: 4.3 x 4.3 x 4.6
– Dennis Lyubyvy
Mar 8 at 15:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050024%2fhow-to-get-values-from-dataframe-with-dynamic-columns%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
May be something like:
m=df.loc[:,df.filter(like='item').columns]
df['Item1']=m.filter(like='0').astype(float).prod(axis=1)
df['Item2']=m.filter(like='1').astype(float).prod(axis=1)
Output:
index Loadedwith item_0L item_0B item_0H item_1L item_1B item_1H Item1 Item2
1 1 4.6 4.3 4.3 4.6 4.3 4.3 85.054 85.054
EDIT
df['Item1']=m.astype(str).filter(like='0').apply(lambda x: 'X'.join(x),axis=1)
df['Item2']=m.astype(str).filter(like='1').apply(lambda x: 'X'.join(x),axis=1)
print(df)
index Loadedwith item_0L item_0B item_0H item_1L item_1B item_1H
0 1 1 4.6 4.3 4.3 4.6 4.3 4.3
Item1 Item2
0 4.6X4.3X4.3 4.6X4.3X4.3
I am sorry, but I did not want the float product of the values, but the string values in a form like 4.6x4.3x4.3 with the letter x in between. I have edited the question, apologies for the time wasted because of unclear question.
– Rohit Kumar
Mar 8 at 7:42
1
@RohitKumar how about the section under EDIT?
– anky_91
Mar 8 at 7:58
Thanks for the reply, is there any way we can make the part where you saydf['Item2']
in a dynamic way like df[Item + str(i)] or something ?
– Rohit Kumar
Mar 8 at 10:57
1
i think you can but again, since the filters are changing, for loops may be involved. if possible for you, you can try posting a fresh question for this. :)
– anky_91
Mar 8 at 10:59
1
@RohitKumar unforunately cannot access the link, i use a corporate laptop mat be that's why
– anky_91
Mar 8 at 11:06
|
show 2 more comments
May be something like:
m=df.loc[:,df.filter(like='item').columns]
df['Item1']=m.filter(like='0').astype(float).prod(axis=1)
df['Item2']=m.filter(like='1').astype(float).prod(axis=1)
Output:
index Loadedwith item_0L item_0B item_0H item_1L item_1B item_1H Item1 Item2
1 1 4.6 4.3 4.3 4.6 4.3 4.3 85.054 85.054
EDIT
df['Item1']=m.astype(str).filter(like='0').apply(lambda x: 'X'.join(x),axis=1)
df['Item2']=m.astype(str).filter(like='1').apply(lambda x: 'X'.join(x),axis=1)
print(df)
index Loadedwith item_0L item_0B item_0H item_1L item_1B item_1H
0 1 1 4.6 4.3 4.3 4.6 4.3 4.3
Item1 Item2
0 4.6X4.3X4.3 4.6X4.3X4.3
I am sorry, but I did not want the float product of the values, but the string values in a form like 4.6x4.3x4.3 with the letter x in between. I have edited the question, apologies for the time wasted because of unclear question.
– Rohit Kumar
Mar 8 at 7:42
1
@RohitKumar how about the section under EDIT?
– anky_91
Mar 8 at 7:58
Thanks for the reply, is there any way we can make the part where you saydf['Item2']
in a dynamic way like df[Item + str(i)] or something ?
– Rohit Kumar
Mar 8 at 10:57
1
i think you can but again, since the filters are changing, for loops may be involved. if possible for you, you can try posting a fresh question for this. :)
– anky_91
Mar 8 at 10:59
1
@RohitKumar unforunately cannot access the link, i use a corporate laptop mat be that's why
– anky_91
Mar 8 at 11:06
|
show 2 more comments
May be something like:
m=df.loc[:,df.filter(like='item').columns]
df['Item1']=m.filter(like='0').astype(float).prod(axis=1)
df['Item2']=m.filter(like='1').astype(float).prod(axis=1)
Output:
index Loadedwith item_0L item_0B item_0H item_1L item_1B item_1H Item1 Item2
1 1 4.6 4.3 4.3 4.6 4.3 4.3 85.054 85.054
EDIT
df['Item1']=m.astype(str).filter(like='0').apply(lambda x: 'X'.join(x),axis=1)
df['Item2']=m.astype(str).filter(like='1').apply(lambda x: 'X'.join(x),axis=1)
print(df)
index Loadedwith item_0L item_0B item_0H item_1L item_1B item_1H
0 1 1 4.6 4.3 4.3 4.6 4.3 4.3
Item1 Item2
0 4.6X4.3X4.3 4.6X4.3X4.3
May be something like:
m=df.loc[:,df.filter(like='item').columns]
df['Item1']=m.filter(like='0').astype(float).prod(axis=1)
df['Item2']=m.filter(like='1').astype(float).prod(axis=1)
Output:
index Loadedwith item_0L item_0B item_0H item_1L item_1B item_1H Item1 Item2
1 1 4.6 4.3 4.3 4.6 4.3 4.3 85.054 85.054
EDIT
df['Item1']=m.astype(str).filter(like='0').apply(lambda x: 'X'.join(x),axis=1)
df['Item2']=m.astype(str).filter(like='1').apply(lambda x: 'X'.join(x),axis=1)
print(df)
index Loadedwith item_0L item_0B item_0H item_1L item_1B item_1H
0 1 1 4.6 4.3 4.3 4.6 4.3 4.3
Item1 Item2
0 4.6X4.3X4.3 4.6X4.3X4.3
edited Mar 8 at 7:58
answered Mar 7 at 18:38
anky_91anky_91
10.6k2922
10.6k2922
I am sorry, but I did not want the float product of the values, but the string values in a form like 4.6x4.3x4.3 with the letter x in between. I have edited the question, apologies for the time wasted because of unclear question.
– Rohit Kumar
Mar 8 at 7:42
1
@RohitKumar how about the section under EDIT?
– anky_91
Mar 8 at 7:58
Thanks for the reply, is there any way we can make the part where you saydf['Item2']
in a dynamic way like df[Item + str(i)] or something ?
– Rohit Kumar
Mar 8 at 10:57
1
i think you can but again, since the filters are changing, for loops may be involved. if possible for you, you can try posting a fresh question for this. :)
– anky_91
Mar 8 at 10:59
1
@RohitKumar unforunately cannot access the link, i use a corporate laptop mat be that's why
– anky_91
Mar 8 at 11:06
|
show 2 more comments
I am sorry, but I did not want the float product of the values, but the string values in a form like 4.6x4.3x4.3 with the letter x in between. I have edited the question, apologies for the time wasted because of unclear question.
– Rohit Kumar
Mar 8 at 7:42
1
@RohitKumar how about the section under EDIT?
– anky_91
Mar 8 at 7:58
Thanks for the reply, is there any way we can make the part where you saydf['Item2']
in a dynamic way like df[Item + str(i)] or something ?
– Rohit Kumar
Mar 8 at 10:57
1
i think you can but again, since the filters are changing, for loops may be involved. if possible for you, you can try posting a fresh question for this. :)
– anky_91
Mar 8 at 10:59
1
@RohitKumar unforunately cannot access the link, i use a corporate laptop mat be that's why
– anky_91
Mar 8 at 11:06
I am sorry, but I did not want the float product of the values, but the string values in a form like 4.6x4.3x4.3 with the letter x in between. I have edited the question, apologies for the time wasted because of unclear question.
– Rohit Kumar
Mar 8 at 7:42
I am sorry, but I did not want the float product of the values, but the string values in a form like 4.6x4.3x4.3 with the letter x in between. I have edited the question, apologies for the time wasted because of unclear question.
– Rohit Kumar
Mar 8 at 7:42
1
1
@RohitKumar how about the section under EDIT?
– anky_91
Mar 8 at 7:58
@RohitKumar how about the section under EDIT?
– anky_91
Mar 8 at 7:58
Thanks for the reply, is there any way we can make the part where you say
df['Item2']
in a dynamic way like df[Item + str(i)] or something ?– Rohit Kumar
Mar 8 at 10:57
Thanks for the reply, is there any way we can make the part where you say
df['Item2']
in a dynamic way like df[Item + str(i)] or something ?– Rohit Kumar
Mar 8 at 10:57
1
1
i think you can but again, since the filters are changing, for loops may be involved. if possible for you, you can try posting a fresh question for this. :)
– anky_91
Mar 8 at 10:59
i think you can but again, since the filters are changing, for loops may be involved. if possible for you, you can try posting a fresh question for this. :)
– anky_91
Mar 8 at 10:59
1
1
@RohitKumar unforunately cannot access the link, i use a corporate laptop mat be that's why
– anky_91
Mar 8 at 11:06
@RohitKumar unforunately cannot access the link, i use a corporate laptop mat be that's why
– anky_91
Mar 8 at 11:06
|
show 2 more comments
I'm a bit confused so I apologize if this is general, but it seems like you either need to parse the data or iterate through it. I'd recommend something along the lines of these:
Parse Line
f = open(file, "r")
line = f.readline()
data = []
while len(line) != 0:
data.append(line.strip(","))
//other code and stuff
line = f.readline()
f.close()
This will open a file and will read the data and form a list of lists based off of the data. In doing this it becomes very easy to iterate through the list, making a segway into iterating.
Iterating
Should you need to iterate through a list of you values, a for loop is the easiest way. If you need to quickly get the entire row or column, though, I'd recommend
data = [your data]
row = data[0][:]
column = data[:][0]
just replace the 0 with whatever index you need. NOTE: This will only work with two-dimensional lists, which is why I recommend parsing as I previously showed.
Edit: You can find more examples with this by looking into list comprehension and list splicing
Hm...thanks for the new direction, though can you guide me through where I am might be going wrong with mine?
– Rohit Kumar
Mar 7 at 18:09
@RohitKumar there are a few optimizations I'd make. First, trypandas.read_csv
first to get your data and then take a look atpandas.loc[]
next. Pandas Doc
– Gabe Ron
Mar 7 at 18:20
add a comment |
I'm a bit confused so I apologize if this is general, but it seems like you either need to parse the data or iterate through it. I'd recommend something along the lines of these:
Parse Line
f = open(file, "r")
line = f.readline()
data = []
while len(line) != 0:
data.append(line.strip(","))
//other code and stuff
line = f.readline()
f.close()
This will open a file and will read the data and form a list of lists based off of the data. In doing this it becomes very easy to iterate through the list, making a segway into iterating.
Iterating
Should you need to iterate through a list of you values, a for loop is the easiest way. If you need to quickly get the entire row or column, though, I'd recommend
data = [your data]
row = data[0][:]
column = data[:][0]
just replace the 0 with whatever index you need. NOTE: This will only work with two-dimensional lists, which is why I recommend parsing as I previously showed.
Edit: You can find more examples with this by looking into list comprehension and list splicing
Hm...thanks for the new direction, though can you guide me through where I am might be going wrong with mine?
– Rohit Kumar
Mar 7 at 18:09
@RohitKumar there are a few optimizations I'd make. First, trypandas.read_csv
first to get your data and then take a look atpandas.loc[]
next. Pandas Doc
– Gabe Ron
Mar 7 at 18:20
add a comment |
I'm a bit confused so I apologize if this is general, but it seems like you either need to parse the data or iterate through it. I'd recommend something along the lines of these:
Parse Line
f = open(file, "r")
line = f.readline()
data = []
while len(line) != 0:
data.append(line.strip(","))
//other code and stuff
line = f.readline()
f.close()
This will open a file and will read the data and form a list of lists based off of the data. In doing this it becomes very easy to iterate through the list, making a segway into iterating.
Iterating
Should you need to iterate through a list of you values, a for loop is the easiest way. If you need to quickly get the entire row or column, though, I'd recommend
data = [your data]
row = data[0][:]
column = data[:][0]
just replace the 0 with whatever index you need. NOTE: This will only work with two-dimensional lists, which is why I recommend parsing as I previously showed.
Edit: You can find more examples with this by looking into list comprehension and list splicing
I'm a bit confused so I apologize if this is general, but it seems like you either need to parse the data or iterate through it. I'd recommend something along the lines of these:
Parse Line
f = open(file, "r")
line = f.readline()
data = []
while len(line) != 0:
data.append(line.strip(","))
//other code and stuff
line = f.readline()
f.close()
This will open a file and will read the data and form a list of lists based off of the data. In doing this it becomes very easy to iterate through the list, making a segway into iterating.
Iterating
Should you need to iterate through a list of you values, a for loop is the easiest way. If you need to quickly get the entire row or column, though, I'd recommend
data = [your data]
row = data[0][:]
column = data[:][0]
just replace the 0 with whatever index you need. NOTE: This will only work with two-dimensional lists, which is why I recommend parsing as I previously showed.
Edit: You can find more examples with this by looking into list comprehension and list splicing
answered Mar 7 at 18:04
Gabe RonGabe Ron
116
116
Hm...thanks for the new direction, though can you guide me through where I am might be going wrong with mine?
– Rohit Kumar
Mar 7 at 18:09
@RohitKumar there are a few optimizations I'd make. First, trypandas.read_csv
first to get your data and then take a look atpandas.loc[]
next. Pandas Doc
– Gabe Ron
Mar 7 at 18:20
add a comment |
Hm...thanks for the new direction, though can you guide me through where I am might be going wrong with mine?
– Rohit Kumar
Mar 7 at 18:09
@RohitKumar there are a few optimizations I'd make. First, trypandas.read_csv
first to get your data and then take a look atpandas.loc[]
next. Pandas Doc
– Gabe Ron
Mar 7 at 18:20
Hm...thanks for the new direction, though can you guide me through where I am might be going wrong with mine?
– Rohit Kumar
Mar 7 at 18:09
Hm...thanks for the new direction, though can you guide me through where I am might be going wrong with mine?
– Rohit Kumar
Mar 7 at 18:09
@RohitKumar there are a few optimizations I'd make. First, try
pandas.read_csv
first to get your data and then take a look at pandas.loc[]
next. Pandas Doc– Gabe Ron
Mar 7 at 18:20
@RohitKumar there are a few optimizations I'd make. First, try
pandas.read_csv
first to get your data and then take a look at pandas.loc[]
next. Pandas Doc– Gabe Ron
Mar 7 at 18:20
add a comment |
This solution will leverage pd.melt
function and create a table where each line would be a combination of a Truck (Index) and an Item Number
df = pd.read_csv('df.csv')
# We will operate on a subset of columns, leaving just index and columns we need
truck_level_df = df.drop(['Name', 'TruckID', 'Length', 'Breadth',
'Height', 'Volume', 'Weight', 'Price', 'Quantity', 'Loaded with'],
axis = 1)
truck_level_df:
index item_0L item_0B item_0H item_1L item_1B item_1H
0 1 4.6 4.3 4.3 4.6 4.3 4.3
# Create table with all the items and their measures
item_measure_level_df = truck_level_df.melt(id_vars = 'index',
var_name = 'item_id_and_measure', value_name = 'item_val')
# Remove unneeded substring
item_measure_level_df['item_id_and_measure'] =
item_measure_level_df['item_id_and_measure'].str.replace('item_', '')
# Extract Item ID
item_measure_level_df['item_id'] =
item_measure_level_df['item_id_and_measure']
.str.replace(r'[A-Z]*', '', case = False)
# Create df where each line is a combination
# of a Truck and an item
item_level_df = item_measure_level_df[['index', 'item_id']].drop_duplicates()
item_level_df:
index item_id_and_measure item_val item_id
0 1 0L 4.6 0
1 1 0B 4.3 0
2 1 0H 4.3 0
3 1 1L 4.6 1
4 1 1B 4.3 1
5 1 1H 4.3 1
Final step:
item_measure_level_df['item_val'] = item_measure_level_df['item_val'].astype('str')
# Group by Item and get LxHxB string
item_level_df['volume_string'] = item_measure_level_df.sort_values(by = ['index','item_id_and_measure']).groupby(['index','item_id'])['item_val'].apply(lambda x: ' x '.join(x)).values
The output:
index item_id volume_string
0 1 0 4.3 x 4.3 x 4.6
3 1 1 4.3 x 4.3 x 4.6
This solution will digest as many groups of columns as you would have
Shared notebook: https://colab.research.google.com/drive/16xUCMCH7rhOOp9Jwlv2RISnnmpzK-06d#scrollTo=lRDVe6B40VsH
I can't seem to run your code, there is an error replace() got an unexpected keyword argument 'regex' . Please use this colab to try your own code.
– Rohit Kumar
Mar 8 at 7:39
colab is using pandas 0.22.0 for some reason, regex was introduced in 0.23
– Dennis Lyubyvy
Mar 8 at 14:54
This is working (I have removed regex parameter and created a regex pattern instead): colab.research.google.com/drive/…
– Dennis Lyubyvy
Mar 8 at 14:55
Also I have posted solution that gives you a string like this: 4.3 x 4.3 x 4.6
– Dennis Lyubyvy
Mar 8 at 15:10
add a comment |
This solution will leverage pd.melt
function and create a table where each line would be a combination of a Truck (Index) and an Item Number
df = pd.read_csv('df.csv')
# We will operate on a subset of columns, leaving just index and columns we need
truck_level_df = df.drop(['Name', 'TruckID', 'Length', 'Breadth',
'Height', 'Volume', 'Weight', 'Price', 'Quantity', 'Loaded with'],
axis = 1)
truck_level_df:
index item_0L item_0B item_0H item_1L item_1B item_1H
0 1 4.6 4.3 4.3 4.6 4.3 4.3
# Create table with all the items and their measures
item_measure_level_df = truck_level_df.melt(id_vars = 'index',
var_name = 'item_id_and_measure', value_name = 'item_val')
# Remove unneeded substring
item_measure_level_df['item_id_and_measure'] =
item_measure_level_df['item_id_and_measure'].str.replace('item_', '')
# Extract Item ID
item_measure_level_df['item_id'] =
item_measure_level_df['item_id_and_measure']
.str.replace(r'[A-Z]*', '', case = False)
# Create df where each line is a combination
# of a Truck and an item
item_level_df = item_measure_level_df[['index', 'item_id']].drop_duplicates()
item_level_df:
index item_id_and_measure item_val item_id
0 1 0L 4.6 0
1 1 0B 4.3 0
2 1 0H 4.3 0
3 1 1L 4.6 1
4 1 1B 4.3 1
5 1 1H 4.3 1
Final step:
item_measure_level_df['item_val'] = item_measure_level_df['item_val'].astype('str')
# Group by Item and get LxHxB string
item_level_df['volume_string'] = item_measure_level_df.sort_values(by = ['index','item_id_and_measure']).groupby(['index','item_id'])['item_val'].apply(lambda x: ' x '.join(x)).values
The output:
index item_id volume_string
0 1 0 4.3 x 4.3 x 4.6
3 1 1 4.3 x 4.3 x 4.6
This solution will digest as many groups of columns as you would have
Shared notebook: https://colab.research.google.com/drive/16xUCMCH7rhOOp9Jwlv2RISnnmpzK-06d#scrollTo=lRDVe6B40VsH
I can't seem to run your code, there is an error replace() got an unexpected keyword argument 'regex' . Please use this colab to try your own code.
– Rohit Kumar
Mar 8 at 7:39
colab is using pandas 0.22.0 for some reason, regex was introduced in 0.23
– Dennis Lyubyvy
Mar 8 at 14:54
This is working (I have removed regex parameter and created a regex pattern instead): colab.research.google.com/drive/…
– Dennis Lyubyvy
Mar 8 at 14:55
Also I have posted solution that gives you a string like this: 4.3 x 4.3 x 4.6
– Dennis Lyubyvy
Mar 8 at 15:10
add a comment |
This solution will leverage pd.melt
function and create a table where each line would be a combination of a Truck (Index) and an Item Number
df = pd.read_csv('df.csv')
# We will operate on a subset of columns, leaving just index and columns we need
truck_level_df = df.drop(['Name', 'TruckID', 'Length', 'Breadth',
'Height', 'Volume', 'Weight', 'Price', 'Quantity', 'Loaded with'],
axis = 1)
truck_level_df:
index item_0L item_0B item_0H item_1L item_1B item_1H
0 1 4.6 4.3 4.3 4.6 4.3 4.3
# Create table with all the items and their measures
item_measure_level_df = truck_level_df.melt(id_vars = 'index',
var_name = 'item_id_and_measure', value_name = 'item_val')
# Remove unneeded substring
item_measure_level_df['item_id_and_measure'] =
item_measure_level_df['item_id_and_measure'].str.replace('item_', '')
# Extract Item ID
item_measure_level_df['item_id'] =
item_measure_level_df['item_id_and_measure']
.str.replace(r'[A-Z]*', '', case = False)
# Create df where each line is a combination
# of a Truck and an item
item_level_df = item_measure_level_df[['index', 'item_id']].drop_duplicates()
item_level_df:
index item_id_and_measure item_val item_id
0 1 0L 4.6 0
1 1 0B 4.3 0
2 1 0H 4.3 0
3 1 1L 4.6 1
4 1 1B 4.3 1
5 1 1H 4.3 1
Final step:
item_measure_level_df['item_val'] = item_measure_level_df['item_val'].astype('str')
# Group by Item and get LxHxB string
item_level_df['volume_string'] = item_measure_level_df.sort_values(by = ['index','item_id_and_measure']).groupby(['index','item_id'])['item_val'].apply(lambda x: ' x '.join(x)).values
The output:
index item_id volume_string
0 1 0 4.3 x 4.3 x 4.6
3 1 1 4.3 x 4.3 x 4.6
This solution will digest as many groups of columns as you would have
Shared notebook: https://colab.research.google.com/drive/16xUCMCH7rhOOp9Jwlv2RISnnmpzK-06d#scrollTo=lRDVe6B40VsH
This solution will leverage pd.melt
function and create a table where each line would be a combination of a Truck (Index) and an Item Number
df = pd.read_csv('df.csv')
# We will operate on a subset of columns, leaving just index and columns we need
truck_level_df = df.drop(['Name', 'TruckID', 'Length', 'Breadth',
'Height', 'Volume', 'Weight', 'Price', 'Quantity', 'Loaded with'],
axis = 1)
truck_level_df:
index item_0L item_0B item_0H item_1L item_1B item_1H
0 1 4.6 4.3 4.3 4.6 4.3 4.3
# Create table with all the items and their measures
item_measure_level_df = truck_level_df.melt(id_vars = 'index',
var_name = 'item_id_and_measure', value_name = 'item_val')
# Remove unneeded substring
item_measure_level_df['item_id_and_measure'] =
item_measure_level_df['item_id_and_measure'].str.replace('item_', '')
# Extract Item ID
item_measure_level_df['item_id'] =
item_measure_level_df['item_id_and_measure']
.str.replace(r'[A-Z]*', '', case = False)
# Create df where each line is a combination
# of a Truck and an item
item_level_df = item_measure_level_df[['index', 'item_id']].drop_duplicates()
item_level_df:
index item_id_and_measure item_val item_id
0 1 0L 4.6 0
1 1 0B 4.3 0
2 1 0H 4.3 0
3 1 1L 4.6 1
4 1 1B 4.3 1
5 1 1H 4.3 1
Final step:
item_measure_level_df['item_val'] = item_measure_level_df['item_val'].astype('str')
# Group by Item and get LxHxB string
item_level_df['volume_string'] = item_measure_level_df.sort_values(by = ['index','item_id_and_measure']).groupby(['index','item_id'])['item_val'].apply(lambda x: ' x '.join(x)).values
The output:
index item_id volume_string
0 1 0 4.3 x 4.3 x 4.6
3 1 1 4.3 x 4.3 x 4.6
This solution will digest as many groups of columns as you would have
Shared notebook: https://colab.research.google.com/drive/16xUCMCH7rhOOp9Jwlv2RISnnmpzK-06d#scrollTo=lRDVe6B40VsH
edited Mar 8 at 15:13
answered Mar 7 at 19:49
Dennis LyubyvyDennis Lyubyvy
1116
1116
I can't seem to run your code, there is an error replace() got an unexpected keyword argument 'regex' . Please use this colab to try your own code.
– Rohit Kumar
Mar 8 at 7:39
colab is using pandas 0.22.0 for some reason, regex was introduced in 0.23
– Dennis Lyubyvy
Mar 8 at 14:54
This is working (I have removed regex parameter and created a regex pattern instead): colab.research.google.com/drive/…
– Dennis Lyubyvy
Mar 8 at 14:55
Also I have posted solution that gives you a string like this: 4.3 x 4.3 x 4.6
– Dennis Lyubyvy
Mar 8 at 15:10
add a comment |
I can't seem to run your code, there is an error replace() got an unexpected keyword argument 'regex' . Please use this colab to try your own code.
– Rohit Kumar
Mar 8 at 7:39
colab is using pandas 0.22.0 for some reason, regex was introduced in 0.23
– Dennis Lyubyvy
Mar 8 at 14:54
This is working (I have removed regex parameter and created a regex pattern instead): colab.research.google.com/drive/…
– Dennis Lyubyvy
Mar 8 at 14:55
Also I have posted solution that gives you a string like this: 4.3 x 4.3 x 4.6
– Dennis Lyubyvy
Mar 8 at 15:10
I can't seem to run your code, there is an error replace() got an unexpected keyword argument 'regex' . Please use this colab to try your own code.
– Rohit Kumar
Mar 8 at 7:39
I can't seem to run your code, there is an error replace() got an unexpected keyword argument 'regex' . Please use this colab to try your own code.
– Rohit Kumar
Mar 8 at 7:39
colab is using pandas 0.22.0 for some reason, regex was introduced in 0.23
– Dennis Lyubyvy
Mar 8 at 14:54
colab is using pandas 0.22.0 for some reason, regex was introduced in 0.23
– Dennis Lyubyvy
Mar 8 at 14:54
This is working (I have removed regex parameter and created a regex pattern instead): colab.research.google.com/drive/…
– Dennis Lyubyvy
Mar 8 at 14:55
This is working (I have removed regex parameter and created a regex pattern instead): colab.research.google.com/drive/…
– Dennis Lyubyvy
Mar 8 at 14:55
Also I have posted solution that gives you a string like this: 4.3 x 4.3 x 4.6
– Dennis Lyubyvy
Mar 8 at 15:10
Also I have posted solution that gives you a string like this: 4.3 x 4.3 x 4.6
– Dennis Lyubyvy
Mar 8 at 15:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050024%2fhow-to-get-values-from-dataframe-with-dynamic-columns%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hi Rohit, would would mind creating an example like this Thanks
– anky_91
Mar 7 at 17:53
1
@anky_91 Sure, and thanks for telling me that.
– Rohit Kumar
Mar 7 at 17:54