Create Range Column with duplicate values pandas [duplicate] - python

This question already has answers here:
Pandas DENSE RANK
(4 answers)
pandas group by and assign a group id then ungroup
(3 answers)
Closed 5 years ago.
I have a pandas dataframe with a column, call it range_id, that looks something like this:
range_id
1
1
2
2
5
5
5
8
8
10
10
...
I want to maintain the number buckets (each rows that share values still share values), but make the numbers ascend uniformly. So the new column would like this:
range_id
1
1
2
2
3
3
3
4
4
5
5
...
I could write a lambda function that maps these in such a way to achieve this desired output, but I was wondering if pandas has any sort of built-in functionality to achieve this, as it has always surprised me before in what it is capable of doing. Thanks for the help!

Related

Pandas DataFrame pivot two columns [duplicate]

This question already has answers here:
How do I transpose dataframe in pandas without index?
(3 answers)
Closed 1 year ago.
I have the following DataFrame df
value
type
one
1
two
2
three
3
which I want to reshape such that the desired output would look like that
one
two
three
1
2
3
I used
df.pivot(columns="values", values="type")
which gave me this:
one
two
three
1
nan
nan
nan
2
nan
nan
nan
3
How can I get around the redundancies?
You don't need to pivot the data, you can .Transpose it:
df.set_index('value').T
Out[22]:
value one two three
type 1 2 3

remove all the values from the column and make it blank [duplicate]

This question already has answers here:
How to add an empty column to a dataframe?
(15 answers)
Closed 2 years ago.
I have a dataframe :
a b
1 dasd
2 fsfr12341
3 %%$dasd11
4 &^hkyo1
I need to remove all the values in column b and make it a blank column
a b
1
2
3
4
Kindly help me on this.
thanks alot
Try changing the b column to empty strings '', like this:
df['b'] = ''

How to create dataframe from 2 dataframe if value exist in both dataframe [duplicate]

This question already has answers here:
Pandas Merging 101
(8 answers)
How to filter Pandas dataframe using 'in' and 'not in' like in SQL
(11 answers)
Closed 2 years ago.
I have 2 Pandas Dataframes with one column (ID).
the first one look like this:
ID
1
2
3
4
5
and the second one look like this:
ID
3
4
5
6
7
I want to make a new Dataframe by combining those 2 Dataframes, but only the value that exist on both Dataframe.
This is the result that I want:
ID
3
4
5
can you show me how to do this in the most efficient way with pandas? Thank you

How to aggregate rows in a dataframe [duplicate]

This question already has answers here:
How do I Pandas group-by to get sum?
(11 answers)
Closed 2 years ago.
I have a dataframe that contains values by country (and by region in certain countries) and which looks like this:
For each country that is repeated, I would add the values by regions so that there is only one row per country and obtain the following file:
How can I do this in Python? Since I'm really new to Python, I don't mind having a long set of instructions, as long as the procedure is clear, rather than a single line of code, compacted but hard to understand.
Thanks for your help.
You want to study the split-apply-combine paradigm of Pandas DataFrame manipulation. You can do a lot with it. What you want to do is common, and can be accomplished in one line.
>>> import pandas as pd
>>> df = pd.DataFrame({"foo": ["a","b","a","b","c"], "bar": [6,5,4,3,2]})
>>> df
foo bar
0 a 6
1 b 5
2 a 4
3 b 3
4 c 2
>>> df.groupby("foo").sum()
bar
foo
a 10
b 8
c 2

add a column with a counter per group in pandas [duplicate]

This question already has answers here:
Pandas number rows within group in increasing order
(2 answers)
Generate column of unique ID in pandas
(1 answer)
Closed 4 years ago.
I have a pandas DataFrame in python with one column A with numerical values:
A
11
12
13
12
14
I want to add a column that contains a counter that counts the number of elements per group up to that index in column A, like this:
A B
11 1
12 1
13 1
12 2
14 1
How do I create column B?

Categories