Looking for adding a column to DATAFRAME including filtration of DATAFRAME
up vote
0
down vote
favorite
If I have a Dataframe is like this:
df =
T1 price
0 Today is Tuesday 60
1 Tomorrow is Wednesday 70
2 After Tomorrow is Thursday 80
3 The last day is Friday 90
If I want to get new Dataframe by adding column (independent variable) indicates the price when it is bigger than or equal to 80 (price >=80) using Python 3 "What should I do?"
This is an example to the Dataframe which I am looking for:
df =
T1 price New
0 Today is Tuesday 60 Unknow
1 Tomorrow is Wednesday 70 Unknow
2 After Tomorrow is Thursday 80 80
3 The last day is Friday 90 90
I tried to write some code Using python, but I got only the rows where price is is bigger than or equal to 80 (price >=80) as following:
df =
T1 price
2 After Tomorrow is Thursday 80
3 The last day is Friday 90
I need really for your help to get another independent variable (new) including the price when it is bigger than or equal to 80 (price >=80) and give "Unknown" for the price is less than 80.
python dataframe max
add a comment |
up vote
0
down vote
favorite
If I have a Dataframe is like this:
df =
T1 price
0 Today is Tuesday 60
1 Tomorrow is Wednesday 70
2 After Tomorrow is Thursday 80
3 The last day is Friday 90
If I want to get new Dataframe by adding column (independent variable) indicates the price when it is bigger than or equal to 80 (price >=80) using Python 3 "What should I do?"
This is an example to the Dataframe which I am looking for:
df =
T1 price New
0 Today is Tuesday 60 Unknow
1 Tomorrow is Wednesday 70 Unknow
2 After Tomorrow is Thursday 80 80
3 The last day is Friday 90 90
I tried to write some code Using python, but I got only the rows where price is is bigger than or equal to 80 (price >=80) as following:
df =
T1 price
2 After Tomorrow is Thursday 80
3 The last day is Friday 90
I need really for your help to get another independent variable (new) including the price when it is bigger than or equal to 80 (price >=80) and give "Unknown" for the price is less than 80.
python dataframe max
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
If I have a Dataframe is like this:
df =
T1 price
0 Today is Tuesday 60
1 Tomorrow is Wednesday 70
2 After Tomorrow is Thursday 80
3 The last day is Friday 90
If I want to get new Dataframe by adding column (independent variable) indicates the price when it is bigger than or equal to 80 (price >=80) using Python 3 "What should I do?"
This is an example to the Dataframe which I am looking for:
df =
T1 price New
0 Today is Tuesday 60 Unknow
1 Tomorrow is Wednesday 70 Unknow
2 After Tomorrow is Thursday 80 80
3 The last day is Friday 90 90
I tried to write some code Using python, but I got only the rows where price is is bigger than or equal to 80 (price >=80) as following:
df =
T1 price
2 After Tomorrow is Thursday 80
3 The last day is Friday 90
I need really for your help to get another independent variable (new) including the price when it is bigger than or equal to 80 (price >=80) and give "Unknown" for the price is less than 80.
python dataframe max
If I have a Dataframe is like this:
df =
T1 price
0 Today is Tuesday 60
1 Tomorrow is Wednesday 70
2 After Tomorrow is Thursday 80
3 The last day is Friday 90
If I want to get new Dataframe by adding column (independent variable) indicates the price when it is bigger than or equal to 80 (price >=80) using Python 3 "What should I do?"
This is an example to the Dataframe which I am looking for:
df =
T1 price New
0 Today is Tuesday 60 Unknow
1 Tomorrow is Wednesday 70 Unknow
2 After Tomorrow is Thursday 80 80
3 The last day is Friday 90 90
I tried to write some code Using python, but I got only the rows where price is is bigger than or equal to 80 (price >=80) as following:
df =
T1 price
2 After Tomorrow is Thursday 80
3 The last day is Friday 90
I need really for your help to get another independent variable (new) including the price when it is bigger than or equal to 80 (price >=80) and give "Unknown" for the price is less than 80.
python dataframe max
python dataframe max
asked Nov 16 at 23:44
Elwakdy
86
86
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Try: df['new'] = df['price'].apply(lambda x: x if x >= 80 else 'unknown')
It is hard to read from your unformatted tables but I believe this is what you want.
Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
– Elwakdy
2 days ago
I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
– Elwakdy
2 days ago
Or you can do this:df['price'].apply(lambda x: 1 if x >= 80 else 0)
, you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.
– BernardL
2 days ago
Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
– Elwakdy
2 days ago
Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
– BernardL
2 days ago
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Try: df['new'] = df['price'].apply(lambda x: x if x >= 80 else 'unknown')
It is hard to read from your unformatted tables but I believe this is what you want.
Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
– Elwakdy
2 days ago
I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
– Elwakdy
2 days ago
Or you can do this:df['price'].apply(lambda x: 1 if x >= 80 else 0)
, you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.
– BernardL
2 days ago
Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
– Elwakdy
2 days ago
Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
– BernardL
2 days ago
|
show 1 more comment
up vote
0
down vote
accepted
Try: df['new'] = df['price'].apply(lambda x: x if x >= 80 else 'unknown')
It is hard to read from your unformatted tables but I believe this is what you want.
Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
– Elwakdy
2 days ago
I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
– Elwakdy
2 days ago
Or you can do this:df['price'].apply(lambda x: 1 if x >= 80 else 0)
, you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.
– BernardL
2 days ago
Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
– Elwakdy
2 days ago
Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
– BernardL
2 days ago
|
show 1 more comment
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Try: df['new'] = df['price'].apply(lambda x: x if x >= 80 else 'unknown')
It is hard to read from your unformatted tables but I believe this is what you want.
Try: df['new'] = df['price'].apply(lambda x: x if x >= 80 else 'unknown')
It is hard to read from your unformatted tables but I believe this is what you want.
answered Nov 17 at 0:09
BernardL
1,567828
1,567828
Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
– Elwakdy
2 days ago
I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
– Elwakdy
2 days ago
Or you can do this:df['price'].apply(lambda x: 1 if x >= 80 else 0)
, you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.
– BernardL
2 days ago
Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
– Elwakdy
2 days ago
Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
– BernardL
2 days ago
|
show 1 more comment
Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
– Elwakdy
2 days ago
I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
– Elwakdy
2 days ago
Or you can do this:df['price'].apply(lambda x: 1 if x >= 80 else 0)
, you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.
– BernardL
2 days ago
Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
– Elwakdy
2 days ago
Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
– BernardL
2 days ago
Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
– Elwakdy
2 days ago
Thank you so much, BernardL. If I want to change all numerical values (numbers are unlimited) to 1 and "Unknown" to 0 in "new" column (independent variable) "What should I do?"
– Elwakdy
2 days ago
I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
– Elwakdy
2 days ago
I found the solution df.loc[df.new == "unknown",'new'] = 0; df df.loc[df.new > 0,'new'] = 1; df
– Elwakdy
2 days ago
Or you can do this:
df['price'].apply(lambda x: 1 if x >= 80 else 0)
, you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.– BernardL
2 days ago
Or you can do this:
df['price'].apply(lambda x: 1 if x >= 80 else 0)
, you can see the change made here and it basically means that if the value of the column is more than 80, assign 1 else assign 0. Hope it helped.– BernardL
2 days ago
Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
– Elwakdy
2 days ago
Thank you so much, BernardL. I tested the line code and see the conversion to 1 and 0 happened in another Dataframe "What should I do to convert the values to 1 and "unknown" to 0 in same the DataFrame?
– Elwakdy
2 days ago
Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
– BernardL
2 days ago
Same column you mean? You need to make sure you are assigning the new column to the same dataframe.
– BernardL
2 days ago
|
show 1 more comment
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%2f53346798%2flooking-for-adding-a-column-to-dataframe-including-filtration-of-dataframe%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