convert specific columns values to column_names in pandas [duplicate] - python

This question already has answers here:
How can I pivot a dataframe?
(5 answers)
How to pivot a dataframe in Pandas? [duplicate]
(2 answers)
Closed 1 year ago.
I have a dataframe like this:
index,col1,value
1,A,1
1,B,2
2,A,3
2,D,4
2,C,5
2,B,6
And I would like to convert this dataframe to this:
index,col1_A,col1_B,col1_C,col1_D
1,1,2,np.Nan,np.nan
2,3,4,5,6
The conversion is based on the index column value and for each unique index column, the column values from col1 is converted to column name and its associated value is set to the corresponding value available in value columns.
Currently my solution contains looping by creating subset of df as temporary df based on each index and then starting looping there. I am wondering if there is already builtin solution available for it in pandas. please feel free to suggest.

Related

Filter in dataframe based on array of values [duplicate]

This question already has answers here:
How do I select rows from a DataFrame based on column values?
(16 answers)
Closed 1 year ago.
I have one array (name df) and dataframe (name data).
Array consists of unique id, say df=array([10,11,12]).
And dataframe consists of 3 columns: data, id, value.
I want to filter dataframe in such a way that it should only contain id specified in array
IIUC:
data = data[data["id"].isin(df)]

Pandas: How to replace values in a dataframe based on a conditional [duplicate]

This question already has answers here:
Pandas fill missing values in dataframe from another dataframe
(6 answers)
Closed 1 year ago.
I am trying to replace the values of my second dataframe ('area') with those of my first dataframe ('test').
Image of my inputs:
The catch is that I only want to replace the values that are not NaN, so, for example, area.iloc[0,1] will be "6643.68" rather than "3321.84" but area.iloc[-2,-1] will be "19.66" rather than "NaN". I would have thought I could do something like:
area.loc[test.notnull()] = test
or
area.replace(area.loc[test.notnull()], test.notnull())
But this gives me the error "Cannot index with multidimensional key". Any ideas? This should be simple.
Use fillna like:
area.fillna(test)

Python: transpose and group dataframe [duplicate]

This question already has answers here:
How can I pivot a dataframe?
(5 answers)
Closed 2 years ago.
I have dataframe: table_revenue
how can I transpose the dataframe and have grouping by 'stations_id' to see final result as:
where values of cells is the price, aggregated by exact date (column) for specific 'station_id' (row)
It seems you need pivot_table():
output = input.pivot_table(index='station_id',columns='endAt',values='price',aggfunc='sum',fill_value=0)

Map integers to specific strings in a Pandas column [duplicate]

This question already has answers here:
Remap values in pandas column with a dict, preserve NaNs
(11 answers)
Closed 4 years ago.
A data frame in pandas with two columns "text", "Condition". In "Condition" column it contains numerical values for each row of "text". Values are 1,-1,0. I would like to convert these integer values to text labels, for instance, 1 for positive, -1 for negative and 0 for neutral. How can I achieve that?
If I understood you question correctly here are the following ways you can change your values.
Using Series.map:
df['condition'] = df['condition'].map({1:'positive', -1:'negative', 0:'neutral'})
Using Series.replace:
df['condition'] = df['condition'].replace({1:'positive', -1:'negative', 0:'neutral'}}

Selecting rows in pandas for where a column is equal to certain values [duplicate]

This question already has answers here:
Filter dataframe rows if value in column is in a set list of values [duplicate]
(7 answers)
Closed 4 years ago.
I have a pandas dataframe that looks like this:
From this, I want to grab all the rows for particular Filters (1st column). So for example, I want to grab the rows for F218W, F336W, and F373N.
What is the easiest way to do this in pandas?
In addition if I wanted to grab the rows for those filters but also only for Chip 1, how could I do that easily?
Thanks!
This is a simple slicing:
df[df["# Filter"].isin(["F218W", "F336W","F373N"])]
If the rules across multiple columns, you can simply combine them using &:
df[df["# Filter"].isin(["F218W", "F336W","F373N"]) & (df["Chip"] == 1)]

Categories

Resources