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: 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> I'm new to Pando / Python, so any help would be greatly appreciated. Thank you! Use / Code> Use a Boolean mask and incorporation operator 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
str
method with vector to create
~
, 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
Post a Comment