I want to separate column values with blank characters and divide them into different columns.

Asked 2 years ago, Updated 2 years ago, 39 views

I read the CSV file as DF in Pandas as below.
I'd like to have a different column for the zip code and address in this DF.

Enter a description of the image here

I tried the code below.

gyoumu_add_csv['split'] =gyoumu_add_csv['A'].str.split('')
gyoumu_add_csv['A1'] = gyoumu_add_csv['split'].str.get(0)
gyoumu_add_csv['A2'] = gyoumu_add_csv['split'].str.get(1)

However, I get the following error:

 C:\Users\ichir\anaconda3\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning: 
Value is trying to be set on a copy of a slice from a DataFrame.
Try using.loc [row_indexer, col_indexer] = value installed

How can I separate addresses?
Thank you for your cooperation.

python pandas

2022-09-30 18:00

2 Answers

pandas.Series.str.split reads:

Notes
The handling of the n keyword dependencies on the number of found splits:
+   If found split > n, make first n split only
+   If found split <=n, make all split
+   If for a certificate row the number of found splits <n, append None for padding up to if expand = True

If using expand = True, Series and Index calls return DataFrame and MultiIndex objects, reflectively.
Use of regex = False with a pat as a combined regex will raise an error.
df[['A1', 'A2']]=df['A'].str.split('',n=1,expand=True)


2022-09-30 18:00

import pandas as pd
importio

csv_data='"
A
1-23-20 Nakagami-cho, Akishima City, Tokyo, 196-0022
Ina 446-3 in Akiruno City, Tokyo, 190-0142
2-11-5 Higashi Asakusa, Taito Ward, Tokyo, 111-0025
Elegance Ayase 5 Osuga 4-21-11 Katsushika Ward, Tokyo, 124-0001
Chuo 6-28-5 in Ota Ward, Tokyo, 143-0024
'''
df = pd.read_csv(io.StringIO(csv_data))

# extract and join
df = df.join(df['A'].str.extract(r'(?P<A1> \\d{3}-\d{4})\s+(?P<A2>.*)'))

print(df.to_markdown(index=False))


2022-09-30 18:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.