Linked Questions
327 questions linked to/from How do I create a new column where the values are selected based on an existing column?
29
votes
3
answers
264k
views
if else function in pandas dataframe [duplicate]
I'm trying to apply an if condition over a dataframe, but I'm missing something (error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().)
raw_data = {'...
23
votes
3
answers
65k
views
Pandas: update column values from another column if criteria [duplicate]
I have a DataFrame:
A B
1: 0 1
2: 0 0
3: 1 1
4: 0 1
5: 1 0
I want to update each item column A of the DataFrame with values of column B if value from column A equals 0.
DataFrame I want to get:...
9
votes
3
answers
41k
views
pandas if else conditions on multiple columns [duplicate]
suppose i have below df:
import pandas as pd
data_dic = {
"a": [0,0,1,2],
"b": [0,3,4,5],
"c": [6,7,8,9]
}
df = pd.DataFrame(data_dic)
Result:
a b c
0 0 0 6
1 0 3 7
2 1 4 ...
2
votes
2
answers
9k
views
ValueError (while creating a function in python): The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() [duplicate]
def function(df):
if (df['Total'] > 0) & (df['Total'] <= 50000):
df['X'] = (df['Total']*(2/150)) * 0.2
elif (df['Total'] > 50000):
df['X'] = ((50000*(2/150))*0.2) ...
2
votes
2
answers
5k
views
How to filter pandas dataframe column by multiple conditions [duplicate]
I am trying to find median revenue in 2013 for US, France and Spain. My pandas dataframe looks like
I am using the following code
df[(df.year == 2013) & (df.country == ['US', 'FR', 'ES'])]
and ...
2
votes
1
answer
3k
views
Pandas, If value isin column then make change to another column [duplicate]
I want to do: IF a column contains values , then apply a change to joinkey column, IF a column contains different values, then apply a different change to joinkey column, etc.
IF df.loc[df['product'...
1
vote
3
answers
4k
views
Pandas if-else condition [duplicate]
I am just trying on with Pandas and am not sure why I am not getting the proper output (titanic dataset from seaborn).
Status columns should show "f" where it says female in column "sex&...
2
votes
3
answers
2k
views
Converting an integer value in pandas column to string [duplicate]
I have an example data as below:
datetime x y
2021-05-01. 25. val1
2021-05-02. 50. val2
2021-05-03. 100 val3
2021-05-04. 25. val4
2021-05-05. 100. val5
2021-05-06. 50. val6
I would like to ...
1
vote
2
answers
5k
views
Pandas, Multiple Case Statement, ValueError: either both or neither of x and y should be given [duplicate]
Trying to incorporate the following case statement in my code.
d1['newcol'] = np.where((d1['business_name'] == 'HELP')&(d1['level'] == 'Component'), d1['component'], np.where((d1['business_name'] ...
1
vote
1
answer
4k
views
python, Iterate through a dataframe and check if a value from a list exist in a column in that dataframe [duplicate]
I have a dataframe that contains a column with different submarkets from a city. I need to iterate through that column and check if a value in that row matches any of the entries that could be in the ...
1
vote
3
answers
2k
views
Python Pandas replace values in one column based on conditional in multiple other columns [duplicate]
Working with the dataframe df:
Product_ID | Category_A | Category _B
1232 0 0
1343 Unknown X
2543 Nan 0
2549 Y ...
4
votes
1
answer
2k
views
pandas apply function row wise taking too long is there any alternative for below code [duplicate]
I have a data frame and big function like below and i wanted to apply norm_group function to data frame columns but its taking too much time with apply command. is there any way to reduce the time for ...
0
votes
2
answers
2k
views
Creating new column based on conditions and values from other columns in a pandas dataframe [duplicate]
I have a pandas dataframe that looks like this:
+-----+--------+-------+
| Age | PhysID | PedID |
+-----+--------+-------+
| 28 | 111 | 123 |
| 26 | 111 | 123 |
| 3 | 111 | 123 |
+-...
1
vote
1
answer
3k
views
fill missing value base on other columns pandas [duplicate]
I want to fill missing value base on other columns in pandas.
Here is my table:
Gender Married
Male Yes
Male Yes
Female No
Female No
Male NaN
Female NaN
I to fill ...
0
votes
1
answer
3k
views
Return another column's value based on an IF condition [duplicate]
I'm trying to create the total column in Pandas. How it is calculated is as follows - If nb_months = 1 then take the trans_amt as the value if not set to 0.
Trans_amt
Nb_months
Total
12
1
12
0
5
0
...