This question already has answers here:
Use a list of values to select rows from a Pandas dataframe
(8 answers)
Closed 10 months ago.
I have a pandas dataframe containing data and a python list which contains ids. I want to extract data from the pandas dataframe which matches with the values of list.
ids = ['SW00003062', 'SW00003063', 'SW00003067', 'SW00003072']
Dataframe is this:
You can use isin
out = df[df['id'].isin(ids)]
Related
This question already has answers here:
How to combine multiple rows into a single row with pandas [duplicate]
(1 answer)
Concatenate strings from several rows using Pandas groupby
(8 answers)
Closed 1 year ago.
I have this dataframe of tweets collected with the date they were posted. I would like to know if there is any way to group these tweets by date?
For example all tweets from one day X would be on the same line in the dataframe
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.
This question already has answers here:
Remove duplicates from dataframe, based on two columns A,B, keeping row with max value in another column C
(4 answers)
how do I remove rows with duplicate values of columns in pandas data frame?
(4 answers)
Drop all duplicate rows across multiple columns in Python Pandas
(8 answers)
Closed 2 years ago.
I have an Excel file that contains the following information
and I need to extract that data into a dataframe with unique values from column ID and value 1; so that I will end up with something like this:
I have done the following:
df=pd.read_excel(file)
dfUnique=pd.unique(df[["column ID","value1"]]).values.ravel('K')
but I get the following error:
could not broadcast input array from shape (5,2) into shape (5)
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)
This question already has answers here:
Unpack dictionary from Pandas Column
(2 answers)
Closed 2 years ago.
Can you help me please.
My Pandas Dataframe has a column that contains dictionaries with information, I want to divide it into several columns where each contains specific information by dictionary key.
original:
df.loc[df.index[3],'Information'] = Name:Monika/ Age:21/ City:France/ Job:Doctor/ Date of Birth:1999-04-12
expected:
df.loc[df.index[3],'Name']=Monika
df.loc[df.index[3],'Age']=21
df.loc[df.index[3],'City']=France
what you need is unpacking dictionary from Pandas Column.
Please check out this solution: Unpack dictionary from Pandas Column