This question already has answers here:
Remap values in pandas column with a dict, preserve NaNs
(11 answers)
Replace values in a pandas series via dictionary efficiently
(1 answer)
Closed 4 years ago.
I have a dataframe where i need to change row values if it is present in a dictionary like this:
dict = {"A":Apple:"B":Ball,"C":Cat}
c1 c2 c3
0 A Tree GH
1 B Train GC
2 C Yarn GR
I want the column c1 values to be changed from the dict if it is present.
Related
This question already has answers here:
How can I pivot a dataframe?
(5 answers)
Pivoting a Pandas Dataframe containing strings - 'No numeric types to aggregate' error
(3 answers)
Closed 1 year ago.
Pandas question:
If I have this dataframe:
Member
Value
Group
1
a
AC
1
c
AC
1
d
DF
2
b
AC
2
e
DF
which I would like to transform, using pivot?, to a DataFrame showing occurences of individual elements of the group, like:
x
AC
DF
1
ac
d
2
b
e
I run into "Index contains duplicate values, cannot reshape" if I try:
pivot(index='Member', columns=['Group'], values='Value')
Feel confused over something seemingly very trivial. Can somebody help?
This question already has answers here:
Pandas, Pivot table from 2 columns with values being a count of one of those columns
(2 answers)
Most efficient way to melt dataframe with a ton of possible values pandas
(2 answers)
How to form a pivot table on two categorical columns and count for each index?
(2 answers)
Closed 2 years ago.
am trying to transform the rows and count the occurrences of the values based on groupby the id
Dataframe:
id value
A cake
A cookie
B cookie
B cookie
C cake
C cake
C cookie
expected:
id cake cookie
A 1 1
B 0 2
c 2 1
This question already has answers here:
Pandas: drop a level from a multi-level column index?
(7 answers)
Closed 3 years ago.
I have a dataframe that has a redundant set of columns that I would like to get rid of. My actual use case is a bit convoluted, but the essence can be captured in the following:
my_frame = pd.DataFrame(data={'a':[1,1,3],'b':[7,8,9],'c':[4,5,6],
'subcolumn_1':['A1','A2','A3'],
'subcolumn_2':['B1','B2','B3']})
my_frame.set_index(keys=['subcolumn_1','subcolumn_2'], inplace=True)
my_frame.transpose()
i.e.
subcolumn_1 A1 A2 A3
subcolumn_2 B1 B2 B3
a 1 1 3
b 7 8 9
c 4 5 6
I would like to delete subcolumn_2. However, I cannot do so via the standard method (e.g. by a drop) because subcolumn_2 is a column header, not an actual row.
try droplevel
my_frame.columns = my_frame.columns.droplevel(1)
This question already has answers here:
How to unnest (explode) a column in a pandas DataFrame, into multiple rows
(16 answers)
Split (explode) pandas dataframe string entry to separate rows
(27 answers)
Closed 3 years ago.
Let's assume that I have a pandas dataset and its column A contains n dimensional vectors. I would like to split this column into multiple columns. Basically, my dataset looks like :
A B C
[1,0,2,3,5] ... ...
[4,5,3,2,1] ... ...
.........................
And I want to have :
A0 A1 A2 A3 A4 B C
1 0 2 3 5 ... ...
4 5 3 2 1 ... ...
.......................
I can solve this problem by using apply function and for loops, I think. But, I imagine that there exists a better (faster, easier to read, ...) way to do so.
Edit: My post gets marked as duplicate. But the given answers have a solution which leads to more rows. I want more columns as shown above.
Thanks,
This question already has answers here:
Use a list of values to select rows from a Pandas dataframe
(8 answers)
Filter dataframe rows if value in column is in a set list of values [duplicate]
(7 answers)
Closed 5 years ago.
This is a subset of a dataframe:
index drug_id values
1 le.1 f
2 le.7 h
3 le.10 9
4 le.11 10
5 le.15 S
I am going to remove rows that values in the drug_id column are: le.7, le.10, le.11.
This is my code:
df.drop(df.drug_id[['le.7', 'le.10', 'le.11']], inplace = True )
I also tried this:
df.drop(df.drug_id == ['le.7', 'le.10', 'le.11'], inplace = True )
But none of them worked. Any suggestion ?