I have 2 dataframes:
restaurant_ids_dataframe
Data columns (total 13 columns):
business_id 4503 non-null values
categories 4503 non-null values
city 4503 non-null values
full_address 4503 non-null values
latitude 4503 non-null values
longitude 4503 non-null values
name 4503 non-null values
neighborhoods 4503 non-null values
open 4503 non-null values
review_count 4503 non-null values
stars 4503 non-null values
state 4503 non-null values
type 4503 non-null values
dtypes: bool(1), float64(3), int64(1), object(8)`
and
restaurant_review_frame
Int64Index: 158430 entries, 0 to 229905
Data columns (total 8 columns):
business_id 158430 non-null values
date 158430 non-null values
review_id 158430 non-null values
stars 158430 non-null values
text 158430 non-null values
type 158430 non-null values
user_id 158430 non-null values
votes 158430 non-null values
dtypes: int64(1), object(7)
I would like to join these two DataFrames to make them into a single dataframe using the DataFrame.join() command in pandas.
I have tried the following line of code:
#the following line of code creates a left join of restaurant_ids_frame and restaurant_review_frame on the column 'business_id'
restaurant_review_frame.join(other=restaurant_ids_dataframe,on='business_id',how='left')
But when I try this I get the following error:
Exception: columns overlap: Index([business_id, stars, type], dtype=object)
I am very new to pandas and have no clue what I am doing wrong as far as executing the join statement is concerned.
any help would be much appreciated.
You can use merge to combine two dataframes into one:
import pandas as pd
pd.merge(restaurant_ids_dataframe, restaurant_review_frame, on='business_id', how='outer')
where on specifies field name that exists in both dataframes to join on, and how
defines whether its inner/outer/left/right join, with outer using 'union of keys from both frames (SQL: full outer join).' Since you have 'star' column in both dataframes, this by default will create two columns star_x and star_y in the combined dataframe. As #DanAllan mentioned for the join method, you can modify the suffixes for merge by passing it as a kwarg. Default is suffixes=('_x', '_y'). if you wanted to do something like star_restaurant_id and star_restaurant_review, you can do:
pd.merge(restaurant_ids_dataframe, restaurant_review_frame, on='business_id', how='outer', suffixes=('_restaurant_id', '_restaurant_review'))
The parameters are explained in detail in this link.
Joining fails if the DataFrames have some column names in common. The simplest way around it is to include an lsuffix or rsuffix keyword like so:
restaurant_review_frame.join(restaurant_ids_dataframe, on='business_id', how='left', lsuffix="_review")
This way, the columns have distinct names. The documentation addresses this very problem.
Or, you could get around this by simply deleting the offending columns before you join. If, for example, the stars in restaurant_ids_dataframe are redundant to the stars in restaurant_review_frame, you could del restaurant_ids_dataframe['stars'].
In case anyone needs to try and merge two dataframes together on the index (instead of another column), this also works!
T1 and T2 are dataframes that have the same indices
import pandas as pd
T1 = pd.merge(T1, T2, on=T1.index, how='outer')
P.S. I had to use merge because append would fill NaNs in unnecessarily.
In case, you want to merge two DataFrames horizontally, then use this code:
df3 = pd.concat([df1, df2],axis=1, ignore_index=True, sort=False)
Related
I have two dataframes. One is excel file and another will be created by user inputs. Based on the user inputs and conditions on columns in the 1st dataframe, new columns should be added to 1st dataframe with calculations. I have wrote the code, which was successful for the test data, but the results are not coming to dataframe. Any help?
1st Dataframe:
Data columns (total 9 columns):
Column Non-Null Count Dtype
0 DDO Code 8621 non-null object
1 ULB Name 8621 non-null object
2 Dist. 8621 non-null object
3 Div. 8621 non-null object
4 Kgid No 8621 non-null int64
5 Name Of The Official 8621 non-null object
6 PRAN Number 8621 non-null float64
7 Join Date 8621 non-null datetime64[ns]
8 Present Basic 8621 non-null int64
dtypes: datetime64ns, float64(1), int64(2), object(5)
2nd Dataframe will be created by user inputs:
enter image description here
from the above data, I need to append 'n' columns based on the user inputs with loops and condition.
here is the code:
for a,b in zip(month_data.month_list, month_data.month_range):
for i,x in zip(contr_calc_new["Join Date"],contr_calc_new['Present Basic']):
if i.date().strftime('%Y-%m') == b.date().strftime('%Y-%m'):
contr_calc_new[a] = 0
else:
contr_calc_new[a] = int(((x + (x*rate)//100)*14//100))
this code is working for test data, but the results are not appending to the 1st dataframe by the calculation based on 2nd dataframe.
i need the result should be like below:
if [join date] column is equal to year & month entered by user, it must return zero, else it should return some calculation. Advance thanks for the help.
Finally I found the proper code. Thank you for your replies.
for a,b in zip(month_data.month_list, month_data.month_range):
contr_calc_new[a] = np.where(contr_calc_new['Join Date'].dt.strftime('%Y-%m') == b.date().strftime('%Y-%m'),0,((contr_calc_new['Present Basic'] + (contr_calc_new['Present Basic']*da_rate)//100)*14//100).astype(int))
I have 2 dataframes:
restaurant_ids_dataframe
Data columns (total 13 columns):
business_id 4503 non-null values
categories 4503 non-null values
city 4503 non-null values
full_address 4503 non-null values
latitude 4503 non-null values
longitude 4503 non-null values
name 4503 non-null values
neighborhoods 4503 non-null values
open 4503 non-null values
review_count 4503 non-null values
stars 4503 non-null values
state 4503 non-null values
type 4503 non-null values
dtypes: bool(1), float64(3), int64(1), object(8)`
and
restaurant_review_frame
Int64Index: 158430 entries, 0 to 229905
Data columns (total 8 columns):
business_id 158430 non-null values
date 158430 non-null values
review_id 158430 non-null values
stars 158430 non-null values
text 158430 non-null values
type 158430 non-null values
user_id 158430 non-null values
votes 158430 non-null values
dtypes: int64(1), object(7)
I would like to join these two DataFrames to make them into a single dataframe using the DataFrame.join() command in pandas.
I have tried the following line of code:
#the following line of code creates a left join of restaurant_ids_frame and restaurant_review_frame on the column 'business_id'
restaurant_review_frame.join(other=restaurant_ids_dataframe,on='business_id',how='left')
But when I try this I get the following error:
Exception: columns overlap: Index([business_id, stars, type], dtype=object)
I am very new to pandas and have no clue what I am doing wrong as far as executing the join statement is concerned.
any help would be much appreciated.
You can use merge to combine two dataframes into one:
import pandas as pd
pd.merge(restaurant_ids_dataframe, restaurant_review_frame, on='business_id', how='outer')
where on specifies field name that exists in both dataframes to join on, and how
defines whether its inner/outer/left/right join, with outer using 'union of keys from both frames (SQL: full outer join).' Since you have 'star' column in both dataframes, this by default will create two columns star_x and star_y in the combined dataframe. As #DanAllan mentioned for the join method, you can modify the suffixes for merge by passing it as a kwarg. Default is suffixes=('_x', '_y'). if you wanted to do something like star_restaurant_id and star_restaurant_review, you can do:
pd.merge(restaurant_ids_dataframe, restaurant_review_frame, on='business_id', how='outer', suffixes=('_restaurant_id', '_restaurant_review'))
The parameters are explained in detail in this link.
Joining fails if the DataFrames have some column names in common. The simplest way around it is to include an lsuffix or rsuffix keyword like so:
restaurant_review_frame.join(restaurant_ids_dataframe, on='business_id', how='left', lsuffix="_review")
This way, the columns have distinct names. The documentation addresses this very problem.
Or, you could get around this by simply deleting the offending columns before you join. If, for example, the stars in restaurant_ids_dataframe are redundant to the stars in restaurant_review_frame, you could del restaurant_ids_dataframe['stars'].
In case anyone needs to try and merge two dataframes together on the index (instead of another column), this also works!
T1 and T2 are dataframes that have the same indices
import pandas as pd
T1 = pd.merge(T1, T2, on=T1.index, how='outer')
P.S. I had to use merge because append would fill NaNs in unnecessarily.
In case, you want to merge two DataFrames horizontally, then use this code:
df3 = pd.concat([df1, df2],axis=1, ignore_index=True, sort=False)
I'm having trouble using
.groupby
and
.agg
using a tuple column
here is the .info()
account_aggregates.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 9713 entries, 0 to 9712
Data columns (total 14 columns):
NATIVEACCOUNTKEY 9713 non-null int64
(POLL, sum) 9713 non-null int64
num_cancellations 8 non-null float64
I'm trying to do something like this:
session_deciles2_grouped = account_aggregates.groupby(('POLL','sum'))
and this:
session_deciles22=session_deciles2_grouped[('POLL','sum')].agg(['mean','count'])
but the columns aren't being recognized - I keep getting a key error.
account_aggregates.groupby([('POLL','sum'),]) would be required here.
The reason account_aggregates.groupby(('POLL','sum')) won't work is because ('POLL','sum') is a collection, and groupby reads this as there are a column called POLL and there is a column called sum, and use both columns to do a groupby operation.
when we put ('POLL','sum') in a list, it means to groupby by a column named ('POLL','sum').
Therefore, account_aggregates.groupby([('POLL','sum'),]) or account_aggregates.groupby((('POLL','sum'),)) will work.
I have a dataframe which looks like this:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 198300 entries, 0 to 198299
Data columns (total 3 columns):
var 198300 non-null values
period 198300 non-null values
value 141492 non-null values
dtypes: float64(1), object(2)
I'd like to change i from having three collumns (var, period, value) to having all values of the period variable as columns, the values in var as rows. i try using:
X.pivot(index='var', columns='period', values='value')
But I get this error:
raise ReshapeError('Index contains duplicate entries, '
pandas.core.reshape.ReshapeError: Index contains duplicate entries, cannot reshape
But I've checked in excel, there are no duplicate entries... Any help out there? Thanks
To give this question an answer: usually when pandas objects that there are duplicate entries, it's right. To check this I often use
someseries.value_counts().head()
to see if one found its way in there.
This is probably a trivial query but I can't work it out.
Essentially, I want to be able to filter out noisy tweets from a dataframe below
<class 'pandas.core.frame.DataFrame'>
Int64Index: 140381 entries, 0 to 140380
Data columns:
text 140381 non-null values
created_at 140381 non-null values
id 140381 non-null values
from_user 140381 non-null values
geo 5493 non-null values
dtypes: float64(1), object(4)
I can create a dataframe based on unwanted keywords thus:
junk = df[df.text.str.contains("Swans")]
But what's the best way to use this to see what's left?
df[~df.text.str.contains("Swans")]
You can also use the following two options:
option 1:
df[-df.text.str.contains("Swans")]
option 2:
import numpy as np
df[np.invert(df.text.str.contains("Swans"))]