How to Insert String Value into Specific Column Value on Python Pandas

How to insert string value into specific column value on python pandas?

The Simplest solution to Use replace method as a regex and inplace method to make it permanent in the dataframe.

>>> data['Column A'].replace(['ABC'], 'ABC-', regex=True, inplace=True)
Column A
0 ABC-1
1 ABC-2
2 ABC-3
3 ABC-4

Add string in a certain position in column in dataframe

You can perform the operation you presented on a list but you have a column in a dataframe so its (a bit) different.

So while you can do this:

hash = "355879ACB6"
hash = hash[:4] + '-' + hash[4:]

in order to do this on a dataframe you can do it in at least 2 ways:

consider this dummy df:

    LOCATION    Hash
0 USA 355879ACB6
1 USA 455879ACB6
2 USA 388879ACB6
3 USA 800879ACB6
4 JAPAN 355870BCB6
5 JAPAN 355079ACB6

A. vectorization: the most efficient way


LOCATION Hash new_hash
0 USA 355879ACB6 3558-79ACB6
1 USA 455879ACB6 4558-79ACB6
2 USA 388879ACB6 3888-79ACB6
3 USA 800879ACB6 8008-79ACB6
4 JAPAN 355870BCB6 3558-70BCB6
5 JAPAN 355079ACB6 3550-79ACB6

B. apply lambda: intuitive to implement but less attractive in terms of performance

df['new_hash'] = df.apply(lambda x: x['Hash'][:4]+'-'+x['Hash'][4:], axis=1)

How to insert strings from one column of pandas DataFrame to another column at specific index?

As simple as that:

>>> df[3] = df[3].str[:-1] + ', ' + df['1'] + ')'
>>> df
0 1 2 3
0 746200.0 IP:aWSrjjB foldcauchy foldcauchy(c=3.40, loc=853.32, scale=188436.01, IP:aWSrjjB)
1 1061881.5 IP:joW6uH4 johnsonsu johnsonsu(a=-0.39, b=0.46, loc=715076.10, scale=70401.41, IP:joW6uH4)
2 645000.0 IP:4Q3L2kB foldcauchy foldcauchy(c=3.94, loc=835.77, scale=184545.16, IP:4Q3L2kB)
3 284375.0 IP:WLP1cdn loglaplace loglaplace(c=1.81, loc=-1001.33, scale=701001.33, IP:WLP1cdn)
4 666600.0 IP:kQn348T johnsonsu johnsonsu(a=-0.39, b=0.46, loc=715076.10, scale=70401.41, IP:kQn348T)
5 754678.5 IP:kQn348T loglaplace loglaplace(c=1.93, loc=-1087.33, scale=786087.33, IP:kQn348T)


Pandas make new column from string slice of another column

Combine two columns of text in dataframe in pandas/python

Add a specific character between specific numbers in a string column in pandas datafrane? [closed]

If you have these strings in list use a for loop and for each use the followings:

A = A[:2] + 'A' + A[2:]

If you have them in a column of a pandas dataframe, the following will help. Assume df is your dataframe:

df = pd.DataFrame({'column1': ['16923ABCD' ,'16928ABCD' ,'16917ABCD' ,
'16934ABCD' , '16912ABCD']})

df['column1'] = df['column1'].astype(str).str[:2] + 'A' +

add a string prefix to each value in a string column using Pandas

df['col'] = 'str' + df['col'].astype(str)


>>> df = pd.DataFrame({'col':['a',0]})
>>> df
0 a
1 0
>>> df['col'] = 'str' + df['col'].astype(str)
>>> df
0 stra
1 str0

Python, Pandas: Insert specific value into empty cells (by column)

I've added a NaN to demonstrate this works:


name age
0 john doe 27.0
1 jane doe 29.0
2 marianne mustermann
3 max mustermann 23.0
4 jean dupont
5 final NaN

df['age'] = df['age'].replace('',0)

name age
0 john doe 27.0
1 jane doe 29.0
2 marianne mustermann 0.0
3 max mustermann 23.0
4 jean dupont 0.0
5 final NaN


name age
0 john doe 27.0
1 jane doe 29.0
2 marianne mustermann 0.0
3 max mustermann 23.0
4 jean dupont 0.0

Insert string in one column if a value is present in another column

Thank you again Andrej K. for your help. The below seems to work:

df.loc[df['Srch1'].notna() & df['EWIS Comp Type'].isna(), 'EWIS Comp Type'] = 'CONNECTOR'
df['EWIS Comp Type'].fillna('', inplace=True)
df.loc[df['Srch1'].notna() & (df['EWIS Comp Type'].str.find('CONNECTOR')<0), 'EWIS Comp Type'] = df['EWIS Comp Type'] + ', CONNECTOR'

How to insert a string value in a list that is in a cell in pandas dataframe?

Use list comprehension with boolean DataFrame by compare by 1 and filter array created from columns names:

cols = df.columns.to_numpy()
df['Tags_col'] = [list(cols[x]) for x in df.eq(1).to_numpy()]
print (df)

Dog Cat Rabbit Tags_col
0 0 1 1 [Cat, Rabbit]
1 1 0 0 [Dog]

If performance is not important use DataFrame.apply:

df['Tags_col'] = df.apply(lambda x: list(x.index[x==1]), axis=1)
print (df)
Dog Cat Rabbit Tags_col
0 0 1 1 [Cat, Rabbit]
1 1 0 0 [Dog]

Related Topics

Leave a reply