I need to make a join into tables, and for that, I need to make a column type change but Is not changing. This is my code, the dataset is open source.
import pandas as pd
import json
#Get Data
url = 'https://raw.githubusercontent.com/sthemonica/alura-voz/main/Dados/Telco-Customer-Churn.json'
response = urlopen(url)
strfile = response.read().decode('utf-8', 'replace')
jsonfile = json.loads(strfile)
dft = pd.json_normalize(data=jsonfile)
# My Problem
print('Before', dft['Churn'].dtypes)
dft = dft.astype({'Churn': str})
print('After', dft['Churn'].dtypes)
Result:
Before object
After object
note, I also try:
dft = dft.astype({'Churn': "str"})
dft.astype({'Churn': "str"})
dft.astype({'Churn': str})
What I'm doing wrong? This is the join a what to do:
dtemp = [[0, 'No'],
[1, 'Yes']]
columns_names = ['Id', 'Churn']
df_FromTo = pd.DataFrame(data=dtemp, columns=columns_names)
dft = dft.join(df_FromTo, on='Churn', rsuffix="_del")
Result:
ValueError: You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat
CodePudding user response:
dft = dft.merge(df_FromTo, on='Churn')
CodePudding user response:
See pandas.DataFrame.join
on
argument explanation
Column or index level name(s) in the
caller
to join on the index inother
, otherwise joins index-on-index.
When you do dft.join(df_FromTo, on='Churn', rsuffix="_del")
, caller
is dft
, other
is df_FromTo
, you are joining with dft['Churn']
with df_FromTo.index
You can fix with
dft = dft.join(df_FromTo.set_index('Churn'), on='Churn', rsuffix="_del")