Create a new column with value 1/0 based on other column value in pandas [duplicate] - pandas

This question already has answers here:
Adding a new pandas column with mapped value from a dictionary [duplicate]
(1 answer)
Pandas conditional creation of a series/dataframe column
(13 answers)
Mapping values in place (for example with Gender) from string to int in Pandas dataframe [duplicate]
(3 answers)
Closed 9 hours ago.
I want to create a column with values 1 for female, 0 for male based on the gender column in Pandas.
Is using a for loop efficient?

Related

How do create lists of items for every unique ID in a Pandas DataFrame? [duplicate]

This question already has answers here:
How to get unique values from multiple columns in a pandas groupby
(3 answers)
Python pandas unique value ignoring NaN
(4 answers)
Closed 1 year ago.
Imagine I have a table that looks like this.
original table
How do I convert it into this?
converted table
Attached sample data. Thanks.

how do I split 1 str column into 2 columns in a pandas dataframe [duplicate]

This question already has answers here:
How to split a dataframe string column into two columns?
(11 answers)
Closed 2 years ago.
enter image description here
df['business location'] #column i want to split into 2 columns :
df['longitude'] #and
df['latitude']
df[['longitude','latitude']] = sf['Business Location'].str.split(',')
is giving me error:
ValueError: Must have equal len keys and value when setting with an iterable
how do I split?
This will work
df.assign(
longitude=lambda x: x['Business Location'].str.split(',').str[0],
latitude=lambda x: x['Business Location'].str.split(',').str[1])

How to use nlasgest in pandas? [duplicate]

This question already has answers here:
Pandas max value index
(3 answers)
Closed 2 years ago.
I'm looking for the highest row of a dataframa, actually the idea is to pick the highest value and the index. I'm trying to use this code:
data_q11.nlargest(144,['1980','2010'])
where data_q11 is the dataframe,144 the number os rows in this df and range of columns.
Although the result is returning a empty list of 0 rows and x 31 columns.
There is a function in Pandas for the index of the maximum value:
data_q11['col'].idxmax(axis=1)

Pandas how to display pivoted values UNDER columns [duplicate]

This question already has answers here:
How can I pivot a dataframe?
(5 answers)
How do I change order/grouping/level of pandas MultiIndex columns?
(3 answers)
Closed 3 years ago.
I need the below but with USD and EU under each of Bronze, Gold and Silver. How?
Edit - My question is different because I want the columns ABOVE the values.

pyspark sql dataframe keep only null [duplicate]

This question already has answers here:
Filter Pyspark dataframe column with None value
(10 answers)
Closed 6 years ago.
I have a sql dataframe df and there is a column user_id, how do I filter the dataframe and keep only user_id is actually null for further analysis? From the pyspark module page here, one can drop na rows easily but did not say how to do the opposite.
Tried df.filter(df.user_id == 'null'), but the result is 0 column. Maybe it is looking for a string "null". Also df.filter(df.user_id == null) won't work as it is looking for a variable named 'null'
Try
df.filter(df.user_id.isNull())