Pandas Datafarme : division of a column [duplicate] - python

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

Related

How to extract data from pandas dataframe with multiple values? [duplicate]

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)]

convert specific columns values to column_names in pandas [duplicate]

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.

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)

How to change column name in huge dataframe? [duplicate]

This question already has answers here:
Renaming column names in Pandas
(35 answers)
Closed 2 years ago.
I want to change the column name of the dataframe which has 128 coloumns. I want to iteratively change it. Example, I want it to be facefeat_1,facefeat_2,.....facefeat_128. Help would be appreciated. Thank you
Assume that df is your DataFrame variable. It should be like this.
df.columns = ['facefeat_'+str(i) for i in range(1,129)]

Pandas: Return a new Dataframe with specific non continuous column selection [duplicate]

This question already has answers here:
How to take column-slices of dataframe in pandas
(11 answers)
Closed 6 years ago.
I have a dataframe with 85 columns and something like 10.000 rows.
The first column is Shrt_Desc and the last Refuse_Pct
The new data frame that I want has to have Shrt_Desc, then leave some columns out and then include in series Fiber_TD_(g) to Refuse_Pct
I use:
dfi_3 = food_info.loc[:, ['Shrt_Desc', 'Fiber_TD_(g)':'Refuse_Pct']]
but it gives a syntax error.
Any ideas how can I achieve this?
Thank you.
Borrowing the main idea from this answer:
pd.concat([food_info['Shrt_Desc'], food_info.ix[:, 'Fiber_TD_(g)':]], axis=1)

Categories

Resources