How to use nlasgest in pandas? [duplicate] - pandas

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)

Related

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

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?

split number based df of one column into 2 columns based on white space [duplicate]

This question already has answers here:
Pandas Dataframe: split column into multiple columns, right-align inconsistent cell entries
(3 answers)
How to split a dataframe string column into two columns?
(11 answers)
Closed 4 months ago.
According to the docs https://pandas.pydata.org/docs/reference/api/pandas.Series.str.split.html, I want to split this one column of numbers into 2 columns based on default whitespace. However, the following doesnt appear to do anything.
self.data[0].str.split(expand=True)
The df is of shape (1,1) but would like to split into (1,2)
Output
0
0 1.28353e-02 3.24985e-02
Desired Output
0 1
0 1.28353e-02 3.24985e-02
PS: I dont want to specifically create columns A and B

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.

Pandas - list of unique strings in a column [duplicate]

This question already has answers here:
Find the unique values in a column and then sort them
(8 answers)
Closed 1 year ago.
i have a dataframe column which contains these values:
A
A
A
F
R
R
B
B
A
...
I would like to make a list summarizing the different strings, as [A,B,F,...].
I've used groupby with nunique(), but I don't need counting.
How can I make the list ?
Thanks
unique() is enough
df['col'].unique().tolist()
pandas.Series.nunique() is to return the number of unique items.

Pandas Dataframe: Groupby on first two columns and count the occurence for first column [duplicate]

This question already has answers here:
Pandas, groupby and count
(3 answers)
Closed 2 years ago.
I had a dataset as a result of groupby as :
CUSTID TRANSACTION_ID COUNT
CU_1 TR_1 1
CU_1 TR_2 1
CU_1 TR_3 1
CU_2 TR_4 1
CU_2 TR_5 1
I needed to have result as
CUSTID TOTAL_COUNT
CU_1 3
CU_2 2
Run just:
df.groupby('CUSTID').COUNT.sum()
You need to group by a single column (CUSTID) only, then,
from each group, take COUNT column and compute its sum().
Additional step may be to provide the required name to the resulting
Series. If it is important, append .rename('TOTAL_COUNT')
to the above code.
Yet another step may be to convert this Series into a DataFrame.
To do it, append .to_frame() to the above code.