Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String in Pandas
#1
Hello Once again

I have somethehing like this

Quote:komen_F1=df2.iloc[21,1]// this is cell from excel that i open pd.read.excel
when i do 
Quote:print(type(komen_F1)
I get class str.

when i do 

Quote:komen_F1.replace('\n', ' ')

string still have a new line. why? and how to remove new line?
Reply
#2
Post an example what is in the string.
Try another way

print(komen_F1.translate({10: None}))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
could you execute
print(repr(komen_F1))
and post the result
Reply
#4
Did you assign result of replace to a new variable? replace does not work in place.

If you need to remove newlines from entire column in a dataframe, you can use
df.iloc[:, 1] = df.iloc[:, 1].str.replace("\n", "")
or (removes leading/trailing whitespaces too, but wont remove newline between "words")
df.iloc[:, 1] = df.iloc[:, 1].str.strip()
Reply
#5
@wavic
string is in Polish but it something like this:

Quote:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin nibh augue, suscipit a, 
[i]scelerisque sed, lacinia in, mi. Cras vel lorem
[/i]
and your 


Quote:print(komen_F1.translate({10: None}))
print text without \n :)


@buran

it print text with
Quote:\n
betwwen words



@ zivoni

this is not a colun but only one cell but when do your cose with 'replace' i get:
Quote:'str' object has no attribute 'str'


Thank you for your help
Reply
#6
(Apr-07-2017, 07:27 AM)yamanda Wrote: 'str' object has no attribute 'str'
Yes, .str is used on entire pandas df column/pandas serie. If you need to modify only string in the specific cell, you need to use standard string methods.
Reply
#7
Ok method that wrote @wavic works so the problem solved.

but I have another problem. Is it possible to get information about position of Cell in pandas . For example i am looking for position of cell that is write "GOAL" in sheet?

Thanks
Reply
#8
(Apr-07-2017, 10:41 AM)yamanda Wrote: For example i am looking for position of cell that is write "GOAL" in sheet?
Can use iloc or at to get by position.
Eg:
   name  age  weight  seniority    pay
0    Bob   34     121         13  69000
1   Goal   19      63          2  23300
2    Amy   45      59          5  45000
3  Frank   56      93         12  57000
4   Kaka   23      55          7  45000
5    Udo   27      65          3  39000
6   Vera   31      71          8  45000
7  Betty   22      77          5  45000

>>> work_data.iloc[1, 0]
'Goal'
>>> work_data.iloc[1]['name']
'Goal'
>>> work_data.at[1, 'name']
'Goal'
Reply
#9
Looks like @zivoni has experience with Pandas.

What I have used is a str.translate() method. The argument is a dictionary with ord(character) as a key and the value holds what it will be translated to. str.maketrans() can create that dictionary/translation table for you. 

s = '456'
ss = s.translate(str.maketrans('45', '67'))
print(ss)
Output:
'676'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
With .str you have access to many string manipulation functions, check documentation.

Example with dataframe stolen from snippsat, "searching" cells containing substring in given column:
In [4]: df
Out[4]: 
    name age weight seniority    pay
0    Bob  34    121        13  69000
1   Goal  19     63         2  23300
2    Amy  45     59         5  45000
3  Frank  56     93        12  57000
4   Kaka  23     55         7  45000
5    Udo  27     65         3  39000
6   Vera  31     71         8  45000
7  Betty  22     77         5  45000

In [5]: df.name[df.name.str.contains('Goal')]  # returns series with strings that contains "Goal"
Out[5]: 
1    Goal
Name: name, dtype: object

In [6]: df[df.name.str.contains('Goal')].index  # returns indices of "cells" that contains "Goal"
Out[6]: Int64Index([1], dtype='int64')
contains accepts regular expression, so for exact match you can use "^Goal$".

If you need to find string in any colum, you will probably need to iterate over columns and checks each seperately.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to search for specific string in Pandas dataframe Coding_Jam 1 2,434 Nov-02-2020, 09:35 AM
Last Post: PsyPy
  Parse XML String in Pandas Dataframe creedX 2 6,870 Dec-09-2019, 07:35 PM
Last Post: creedX
  pandas: can we look for the index of a string paul18fr 2 2,208 Jul-31-2019, 08:25 AM
Last Post: paul18fr
  Simple String to Time within a pandas dataframe Ecniv 1 2,512 Jun-14-2019, 03:25 AM
Last Post: scidam
  Converting string the pandas dataframe chrismc 0 2,351 Jan-24-2019, 11:07 AM
Last Post: chrismc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020