I have a dataframe data
with these columns col1
and col1.1
.
data:
ID Col1 Col1.1
1 21 Water, Salt
13 18 Onions
101 30 Replaceable with oil, water, acid
I want data
to be:
data:
ID Col1
1 21: Water, Salt
13 18: Onions
101 30: Replaceable with oil, water, acid
So far, I have:
data['Col'] = ': '.join(str(list(zip(data['Col1'], data['Col1.1']))).split("', ")).replace("'", "").replace("(", "").replace(")", "").replace("[", "").replace("]", "")
NOTE: I get a SettingWithCopyWarning
on running this.
How do I go about this? Thank you all
Use str.cat :
df['Col1'].astype(str).str.cat(df['Col1.1'], sep=': ')
Out:
0 21: Water, Salt
1 18: Onions
2 30: Replaceable with oil, water, acid
You need to assign this back and drop the other column to get exactly the same output:
df['Col1'] = df['Col1'].astype(str).str.cat(df['Col1.1'], sep=': ')
df = df.drop('Col1.1', axis=1)
Or, all in one line, thanks to MaxU :
df['Col1'] = df['Col1'].astype(str).str.cat(df.pop('Col1.1'), sep=': ')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.