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?
Post an example what is in the string.
Try another way
print(komen_F1.translate({10: None}))
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()
@
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
(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.
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
(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'
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'
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.