Home > Blockchain >  How to change datatype for single column for multiple dataframes in pandas?
How to change datatype for single column for multiple dataframes in pandas?

Time:03-22

For example i have 5 dataframes like

df1 df2 df3 df4 df4

In every column there is a column present with name 'phone_no'.

I'm trying to change the datatype of phone_no with for loop, but it's not working. Below is the code

df_all = [df1, df2, df3, df4, df5]

df_all = [i['phone_no'].astype(str) for i in df_all]

when i try to print df_all, then it's returning output like

[2        669263000000.0
 3        313988000000.0
 4        182100000000.0
 12       270449000000.0
 13       109617000000.0
 Name: phone_no, Length: 14042, dtype: object,
 1        466167000000.0
 8        433999000000.0
 9        323820000000.0
 11       823428000000.0
 15       659981000000.0
 Name: phone_no, Length: 13947, dtype: object,
 7        832447000000.0
 14       178296000000.0
 22       145628000000.0
 29       642982000000.0
 48       596803000000.0
 Name: phone_no, Length: 13924, dtype: object,
 0        555314000000.0
 5        110872000000.0
 19       890271000000.0
 34       634257000000.0
 37       125423000000.0
 Name: phone_no, Length: 14112, dtype: object,
 6        314615000000.0
 10       982864000000.0
 23       287164000000.0
 24       746213000000.0
 27       590169000000.0
 Name: phone_no, Length: 13686, dtype: object]

The only issue is that, if i run the above code, it's only returning the phone_no column from every dataframe, it's not returning the complete dataframe. How to resolve this issue?

After that how to perform the below code

df_all = [i['phone_no'].str[0:12] for i in df_all]

Output is

[2        669263000000
 3        313988000000
 4        182100000000
 12       270449000000
 13       109617000000
 Name: phone_no, Length: 14042, dtype: object,
 1        466167000000
 8        433999000000
 9        323820000000
 11       823428000000
 15       659981000000
 Name: phone_no, Length: 13947, dtype: object,
 7        832447000000
 14       178296000000
 22       145628000000
 29       642982000000
 48       596803000000
 Name: phone_no, Length: 13924, dtype: object,
 0        555314000000
 5        110872000000
 19       890271000000
 34       634257000000
 37       125423000000
 Name: phone_no, Length: 14112, dtype: object,
 6        314615000000
 10       982864000000
 23       287164000000
 24       746213000000
 27       590169000000
 Name: phone_no, Length: 13686, dtype: object]

The above code will only return the phone_no column from all the dataframes after performing slicing. It'll miss another columns. How to resolve this issue?

CodePudding user response:

You can pass dictionary to DataFrame.astype for convert only some columns names:

df_all = [i.astype({'phone_no': str}) for i in df_all]

EDIT: You can processing column and assign back:

df_all = [i.assign(phone_no = i['phone_no'].str[0:12]) for i in df_all]
  • Related