python - Append string to some dataframe rows with Pandas -


This is my first question on stackoverflow! Please, be patient :)

I want to add text to some lines in a data frame. The original dataframe looks like this: import into [p]: PD [2]: df = pd.DataFrame ({'name and room': ['excelibur: 1] [3]: DF out [3]: Name and room: room, 'room', 'John: 2 rooms', '1 room', 'Lukas: 5 rooms', '4 rooms', 'Jeremy: 1 room']}) [3] 0 Excalibur: 1 room 1 John: 2 rooms 2 1 room 3 Lukas: 5 rooms 4 4 rooms 5 Jeremy: 1 room

As you are watching, there are few lines in the name Is missing I want to add certain strings (such as "whatever:", no strings make no difference) where no name exists (in this example, lines 2 and 4). The last dataset will look like this: <11>

 in  [11]: df out [11]: name and room 0 Excalibur: 1 room 1 John: 2 rooms 2 Whatever: 1 room 3 Lukas: 5 rooms: 4 rooms: 4 rooms 5 Jeremy: 1 room  

I'm new to Pando / Python, so any help would be greatly appreciated.

Thank you! Use str method with vector to create

/ Code> Use a Boolean mask and incorporation operator ~ , pass it on loc and place existing string in the current value:

 < Code> in [83]]: Df.loc [~ df ['name and room']. ':' And 'room'] DF out [83]: Name and room 0 Excalibur: 1 room 1 John: 2 rooms: Str.contains (':'), 'name and room'] = 'whatever: 2 Whatever: 1 room 3 Lukas: 5 rooms 4 Joe: 4 rooms 5 Jeremy: 1 room  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -