I want to resolve 'DataFrame' object has no attribute's split' error in pandas

Asked 2 years ago, Updated 2 years ago, 87 views

I would like to split the string (the string of numbers in the OID column) separated by the "/" read from CSV in pandas and re-list it, but I can't implement it well because of the error in the title.
I would appreciate it if you could give me some advice.
Thank you for your cooperation.

read_csv Results
                                  OID
0    1618544796/1043253441/1468827215
1    1618544796/1043253441/1468827215
2    1618544796/1043253441/1468827215
3    1618544796/1043253441/1468827215
4    1618544796/1043253441/1468827215
..                                ...
197 NaN
198                        1714135326
199                        1714135326
200                        1714135326
201NaN

[202 rows x 1 column]
import pandas as pd

file0 = "./node_file2.csv"
file1 = "./node_file3.csv"

f0 = pd.read_csv (file0, usecols = [0])
f1 = pd.read_csv (file1, usecols = [0])
read_oid0=pd.read_csv(file0, usecols=[11])
read_oid1 = pd.read_csv (file1, usecols=[8])

split_oid0 = read_oid0.split('/',expand=True)
print(read_oid0)

python python3 pandas csv

2022-09-30 15:52

1 Answers

In the application of this article, you can add .str with the column name.
Separate the Pandas string into multiple columns by delimiting characters or regular expressions

The following is true:

split_oid0=read_oid0['OID'].str.split('/',expand=True)

However, if you do so, you may end up with NaN or None.
If the data was originally like this,

OID
0  1618544796/1043253441/1468827215
1  1618544796/1043253441/1468827215
2  1618544796/1043253441/1468827215
3  1618544796/1043253441/1468827215
4  1618544796/1043253441/1468827215
5NaN
6                        1714135326
7                        1714135326
8                        1714135326
9NaN

Here's the result.

0 12
0  1618544796  1043253441  1468827215
1  1618544796  1043253441  1468827215
2  1618544796  1043253441  1468827215
3  1618544796  1043253441  1468827215
4  1618544796  1043253441  1468827215
5 NaN NaN NaN
61714135326 None None
71714135326 None None
81714135326 None None
9 NaN NaN NaN

If necessary, you can apply this article to empty strings 0.
Exclude (delete)/replace (fill in)/extract NaN in pandas

Below is an empty string.

split_oid0=split_oid0.fillna(')

Here's the result.

0 12
0  1618544796  1043253441  1468827215
1  1618544796  1043253441  1468827215
2  1618544796  1043253441  1468827215
3  1618544796  1043253441  1468827215
4  1618544796  1043253441  1468827215
5
6  1714135326
7  1714135326
8  1714135326
9


2022-09-30 15:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.