简体   繁体   中英

Pandas splitting strings from text and then inputting into a dataframe

Lets say I did an experiment where I drew marbles of 2 color and the results come out like

'Experiment Draw1,Draw2'    
ie: 'Trail1 Yellow-Green'

So I insert the results into a Dataframe and would like to get 3 columns (Experiment, first draw, second draw) How do I efficiently split it so I can plot the results on to the results Dataframe as a number (ie)

import pandas as pd

df=pd.DataFrame({'Data': ['Trail1 Yellow-Green','Sample1 Gold-Blue', 'Sample2 Silver-Gold', 'Test2 Gold-Yellow', 'Test Red-Blue'],})

df2 = df['Data'].apply(lambda x: pd.Series(x.split(' ')))
df3 = df2[1].apply(lambda x: pd.DataFrame(x.split('-')))

axis1=['Red','Orange', 'Yellow', 'Green', 'Blue', 'Gold', 'Silver']
axis2=['Red','Orange', 'Yellow', 'Green', 'Blue', 'Gold', 'Silver']

results=pd.DataFrame(index=axis1, columns=axis2)

Would the best way to add terms into the dataframe be something using a for loop and some code like:

results.ix[df3.loc['Red'], 'Blue'] = 'Y'

#For numerical values

results.ix[df3.loc['Red'], 'Blue'] = 1

You can use the str.extract method:

In [11]: s = df.Data

In [12]: res = s.str.extract("(?P<experiment>.*?) (?P<first>.*?)-(?P<second>.*)")

In [13]: res
Out[13]: 
  experiment   first  second
0     Trail1  Yellow   Green
1    Sample1    Gold    Blue
2    Sample2  Silver    Gold
3      Test2    Gold  Yellow
4       Test     Red    Blue

Then I think you're looking for a pivot_table :

In [14]: res.pivot_table(values='experiment', cols='first', rows='second', 
                         aggfunc=len, fill_value=0)
Out[14]: 
first   Gold  Red  Silver  Yellow
second                           
Blue       1    1       0       0
Gold       0    0       1       0
Green      0    0       0       1
Yellow     1    0       0       0

To reindex have same rows and columns, I think you'l have to reindex:

In [15]: _.reindex(axis1).reindex_axis(axis1, 1).fillna(0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM