Reputation: 1944
I have a dataset which looks like this follows
clean.list_raw_id.head(3)
0 {5177, 5178}
1 {2653, 2655}
2 {2793}
I want to add a column having values 5177,2653 & 2793
How can I do this in python
I am trying to use apply on the column but its not working
I was able to extract elemnts using the following code
[e.strip('{}') for e in clean.list_raw_id[1].split('},{')]
Upvotes: 1
Views: 71
Reputation: 898
df = pd.DataFrame(data={'vals': [{5177, 5178}, {2653, 2655}, {2793}]})
df['new_col'] = df['vals'].apply(lambda x: list(x)[0])
returns
vals new_col
0 {5177, 5178} 5177
1 {2653, 2655} 2653
2 {2793} 2793
Upvotes: 0
Reputation: 862761
Use Series.str.extract
for get first numeric value:
df.data = df.data.str.extract("(\d+)")
print (df)
data
0 5177
1 2653
2 2793
Upvotes: 1
Reputation: 29742
Use pd.Series.str
with apply
. Given df
:
data
0 {5177,5178}
1 {2653,2655}
2 {2793}
df.data = df.data.str.findall("\d+").apply(lambda x:x[0])
print(df)
data
0 5177
1 2653
2 2793
Upvotes: 1