Ad
Split Values In Data Frame Columns
I have a Data Frame name df and I want to remove this '|' in fuel column
id car fuel
1 Mercedes petrol|diesel|gas
2 Audi gas|petrol
So that my data look like this
id car fuel
1 Mercedes petrol
1 Mercedes diesel
1 Mercedes gas
2 Audi gas
2 Audi petrol
This is the code that I tried
df_1 = hb.copy()
df_2 = hb.copy()
df_3 = hb.copy()
df_1['fuel'] = df_1['fuel'].apply(lambda x:x.split('|')[0])
df_2['fuel'] = df_2['fuel'].apply(lambda x:x.split('|')[1])
df_3['fuel'] = df_3['fuel'].apply(lambda x:x.split('|')[2])
And this give IndexError: list index out of range
Ad
Answer
Try this:
df=pd.DataFrame({'car':['Mercedes','Audi'],'fuel':['petrol|diesel|gas','gas|petrol']}) #your dataframe
df2=pd.DataFrame() #new black dataframe
for i in range(0,len(df)): #iterating over df
list1=df.iloc[i,1].split('|') #split each value of 'fuel' and store it in a list
for j in range(0,len(list1)): #iterating over list1
list2={'car':df.iloc[i,0],'fuel':list1[j]} #make a dict of each combination of 'car' and elements of list1-'fuel'
df2=df2.append(list2,ignore_index=True) #append each value to the blank df
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad