Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

python - How to split a string using a dynamic lenght?

df = {'Goals': {0: '0,0,0,0', 1: '0,0', 2: '0,0,0,0,0,0', 3: '0,0', 4: '0,0'},
 'Devices': {0: 'mobile,mobile,mobile,mobile',
  1: 'mobile,mobile',
  2: 'mobile,mobile,mobile,mobile,mobile,mobile',
  3: 'mobile,mobile',
  4: 'mobile,mobile'},
 'Channels': {0: 'Paid Social,Paid Social,Paid Social,Paid Social',
  1: 'Paid Social,Paid Social',
  2: 'Paid Search,Paid Search,Paid Search,Paid Search,Paid Search,Paid Search',
  3: 'Display,Display',
  4: 'Referral,Referral'}}

This is what I am trying

df['New column'] = df['Devices'].str[0:df['Channels'].str.index(',')]

I am trying to get part of a string to create a new column working with pandas but the cut needs to be dynamic so I am using the function index to get the len of the string that I need, however when I run the code showed above I just get a column full of NaN. Could someone explain me what is exactly happening, I did a test using variables instead of plain numbers and it actually works but when I used pandas inside of the str function the code does not returns the result that I want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are two ways of accomplishing this, easily enough. The first thing to note is that simply accessing .str and then doing .split (or the indexing operation you're doing currently) is not enough; neither of these functions allow you to pass in a series.

The first option would be to use a .apply function, in the following way:

df = pd.DataFrame({'Devices': ['123,123', '1234,123'], 'index': [3, 4]})

def split(row):
    return row['string_column'][0:row['index']]

df['new_string_column'] = df.apply(split, axis=1)

Another option would be to use Mito, which, full disclaimer, I am a creator of. Mito is a spreadsheet that allows you to write standard Excel formulas, and then generates corresponding Python code for you. In this case, you could just use the formula =LEFT(Devices, FIND(Devices, ",")). It does this all as an extension to a Jupyter Notebook.

Let me know if you have any questions about either of these!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...