reading each line of text to separate pandas columns

Viewed 166

I have a text dataset (.txt) in form row_index a_dic id text. Here is an example

0     {'1': 0.27, '2': 0.68, '0': 0.5} 6081bdea52c838000aaa53d3 "some text" 

I need to separate this by a delimiter so I can read into a pandas data frame. For example

0     {'1': 0.27, '2': 0.68, '0': 0.5}, 6081bdea52c838000aaa53d3, "some text" 

I have tried

for idx, row in data.iterrows():
    row.str.split(pat=r"\"\}", expand=True)
data

but that doesn't seem to do anything.

1 Answers

So, with the following dataframe:

import pandas as pd

df = pd.DataFrame(
    {
        0: [
            "0     {'1': 0.27, '2': 0.68, '0': 0.5} 6081bdea52c838000aaa53d3 some text",
            "1     {'1': 0.32, '2': 0.72, '0': 0.9} 6981bfga52c549000aaa12z9 other text",
        ],
    }
)
print(df)
# Output
                                                                            0
0   0     {'1': 0.27, '2': 0.68, '0': 0.5} 6081bdea52c838000aaa53d3 some text
1  1     {'1': 0.32, '2': 0.72, '0': 0.9} 6981bfga52c549000aaa12z9 other text

You could do this:

df = (
    df[0]
    .str.split(pat=r"(.*)({.*}) (\S*) (.*)", expand=True)
    .drop(columns=[0, 1, 5])
    .T.reset_index(drop=True)
    .T
)

print(df)
# Output
                                  0                         1           2
0  {'1': 0.27, '2': 0.68, '0': 0.5}  6081bdea52c838000aaa53d3   some text
1  {'1': 0.32, '2': 0.72, '0': 0.9}  6981bfga52c549000aaa12z9  other text
Related