Заменить элементы, если они есть в списке - PullRequest
1 голос
/ 10 июля 2020

Я хочу заменить значения фрейма данных столбца, если они находятся в списке. Итак, я попробовал:

to_remove = ['Name (Optional)', 'Date of Birth',
   'Please enter your phone number.']

df_merged['Section'].map({to_remove: None})

Но вернулся:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-820-a080ccbc0440> in <module>
----> 1 df_merged['Section'].map({to_remove: None})

TypeError: unhashable type: 'list'

Обновление

Я попробовал:

df_merged['Section'].map({k: None for k in to_remove})

Но, похоже, только обработать обратно NaN:

0     NaN
1     NaN
2     NaN
3     NaN
4     NaN
5     NaN

Фактические данные здесь

И фактические значения, которые я хочу удалить:

to_remove = ['Name (Optional)', 'Date of Birth',
       'Please enter your phone number.','Please specify which city in particular you would like to travel to this year.',
       'What is your country of residence?',
       'What are the main additions you would like to see be made to your airline experience?',
       'If assistance is required for any health issues, age or pregnancy, what support would you expect? ',
       'What doubts did you have/would you have before booking a hotel room?',
       'Why?', 'Anything else that you would like to share with us',
       'Age',
       'Where do you remember seeing ads in your day to day lives?',
       'Has an advertisement on the TV or Internet ever led you to visit a certain location? If yes, which location?',
       'What would you improve about the Airline XYZ?',
       'Please state why you decided to choose that video.',
       'Email (Optional)',
       'Where was your most recent destination?',
       'Please state your purpose for business travel',
       'What would be your first holiday destination?', 'COVID-19',
       'What precautions did you take for yourself, if any, when you travelled during the outbreak? (Enter N/A if you did not travel during the outbreak)',
       'What precautions would you take yourself, if any, when flying again?',
       'Please tell us any further information/comments that you may have about your intentions to fly again and any other expectations you may have that we should consider when we start flying again. (Please enter N/A if you do not have any further comments)',
       '7 - Based on your previous answers, think about one emotion which you have felt in low intensity and another one in high intensity, could you describe the reason why? ',
       '9 - Based on your previous answers, think about one emotion which you have felt in low intensity and another one in high intensity, could you describe the reason why? ',
       '20 - Due covid-19, what changes are you expecting onboard?',
       '22  - Do you have any concerns  about air travel?',
       'Please enter your email address.',
       'Do you have any other comments, questions or concerns we can help with?',
       'In your experience, what services could have been improved  by the Other airline you selected?',
       'Country where you are living.',
       'Any improvement you would suggest to maintain a better environment of the hotel to make you feel more pleasant to stay?',
       'Collecting your demographic data allows us to keep the data we record unbiased and representative of the entire public.',
       'What countries do you consider  a safe choice this year regarding Corona virus? Please list the countries.',
       'In your own words, please describe what comes to mind when seeing this image.',
       'Which city are you staying?',
       'What suggestions do you have for Global Hotel Chain ABC?',
       'Right communication, matters! ',
       'Do you have any suggestions to help Arline XYZ to improve its services?',
       'What are all the preferences do you expect from XYZ Airlines, due to COVID-19?',
       'Any other comments that you want to add?',
       'Any additional comments?',
       'What other factors, if any, govern your decisions when choosing an airline?',
       'How old are you? (in years)', 'Surname',
       'Do you have any suggestions for, or comments on how social distancing can be implemented?',
       'Have we missed anything? Please use this space to tell us about your preferences which we may have missed.',
       'If you have any comments about what you expect from the hotel industry following the Covid-19 outbreak, please leave them here.',
       'What are your travel plans for the next two years?',
       'Would you like to specify your answer to the previous question?',
       'What is your age?',
       'Is there anything else you would like us to know or take into account when making our decisions for the future?',
       'What country do you reside in?',
       'Please type the country where you live?',
       'Do you think Covid-19 has affected whether or not you will book hotel accomodation in the future? Please explain your reasons in a few sentences.',
       'What is your occupation?',
       'What were your usual activities during the pandemic?',
       'Which online information source usually plays a decisive role in the final choice of your hotel accommodation?',
       'What are your additional thoughts on how the hotel should adapt after the current situation?',
       'When you think of hotel chains, which 3 hotels come to mind?',
       'Please specify which hotel chain you most frequently stay at.',
       'Please state your residential location? (State the City or County)',
       'Do you have any further comments about Airline XYZ, regarding marketing, communication and safety measures post COVID- 19? (Please type your answer into the box below)',
       'Please provide a reason for your previous answer.',
       'Please provide any additional information on your new air travel considerations post-Covid-19.',
       'Is there anything else not on that list that you deem important when booking a hotel?',
       'How will your travel intentions change after COVID-19? (Please give as much detail as possible, e.g., budget constraints, travel destination etc.)',
       'What kinds of practice you wished to see in hotels',
       'What could airline do to make you feel more safe on an airplane?',
       'What words first come to mind when you think of Airline XYZ?',
       'In a few words describe how you normally discover new travel destinations?',
       '5. Do you have any concerns, complaints or suggestions on how accommodation providers can adjust post-Coronavirus?',
       'Please create an ID number with your initials and date of birth (eg: AB15031960)',
       'What would be for you the number one reason to choose a hotel over an other?',
       'Please specify:', 'What is your education level?',
       'Where are you from?',
       '30) (only if you answered "Yes" to question 29) How many children under the age of 18 live in your household? ',
       'If no, please state why below:',
       'For any facilities you would be prepared to use (from previous question), what precautions would be expected to be in place, to encourage use of these facilities?',
       'What type of content and messages would you like to see and receive from the hotel?',
       'It would be very helpful if you could give us some information about yourself to help us put your answers in context:',
       'What does the number of travel days depend on?',
       'Any suggestion you would give to airline company to increase the likelihood of you travelling?',
        'How could an airline improve the aesthetic value of flying?',
       'Describe your ideal business travel (preferably from the moment of entry into the airport until the moment of landing at the destination location)',
       'Which European Exclusive flights companies do you usually travel with?',
        'Do you have any suggestion to help Airline XYZ to improve its safety measures?',
       'Do you have any other tips to help Airline XYZ to encourage you to travel?',
        'Are there any other features not covered that you personally value?',
       'Please enter your email address below if you would like to be entered into the draw.',
       'What changes would you like to see on your flight in the future? If any.',
       'Provide one suggestion that you think should be considered by all airlines after Covid-19 crisis ?',
       'How often do you use accommodation in a calendar year?',
       'What is your monthly income after taxes?',
       'What is your current impression about XYZ Airlines?',
       "By watching this Ad, what do you think about XYZ Airline's safety measures? Please explain briefly.",
       'Why do you think this Ad is not much attractive?', 
        'Question 11. If "Decrease", why? If this does not apply please skip this question. ',
       'Question 35. In order to understand where people who have taken part in this research live we require your full postcode.',
       'Describe your previous during the flight experiences?',
       'What is your biggest concern with air travel currently?',
       'What can Airline XYZ do to improve your flying experience in the future (e.g., highlighting additional hygiene measures)?',
       'If you wish, please provide feedback on how we can improve our surveys in the future.',
       'Which factors are most important to you when booking a holiday? (Please list 3.)',
       'When choosing an airline to fly with, which factors are most important to you? (Please list 3.)',
       'Please list three countries you wish to visit most in the next three years.',
       'Before COVID-19, on average how many times a year would you travel by flight?',
       'We hope to see your thoughts and opinions as a result of our changing world and the influence of media.',
       'We would like to understand what actions you see yourself taking the future, so we can prepare ourselves and adjust our services to accomodate this.',
       'We would like to hear directly back from you about what you think we should be doing and how we can change to adapt for the future.',
       'If you do not fly, why not?',
       'Please elaborate on your response to the question above.',
       'Depending on your response to the question above, please briefly explain why you think this.',
       "If you answered 'Yes' to the question above, could you briefly explain where you are planning on travelling to and for what purpose (leisure, business, family)?",
       'Briefly explain what you value MOST about the news sources that you choose to consume.',
       'Consumer Intention', 'Consumer Behavior', 'ABC hotel balcony.',
       'How do you think hospitality measures should change after this trauma?',
       'Our usual view from top floors in major cities.',
       'Did any of your friends, family or any close ones recommend us to you?',
       'Describe your best and worst time here.',
       'In what country are you based?',
       'Please list the 3 most important things that you expect from an airline',
       'Which company do you travel more often with?',
       'When are you planning to travel next?',
       'What comes to mind first when you think about travelling with us in the near future?',
       'Please give details of what you would like to see improve or what prevents you from traveling with us:',
       'Consumer Traits', 'Which country are you from?',
       'Which type of cancellation policy will most appropriate?',
       'How many trips have you made in the last 12 months?',
       'Do you have any other suggestions how Airline XYZ could adjust their service following the COVID-19 pandemic',
       'Gender', 'Any other feedback/concerns:',
       'Is there anything else that you feel we, as a hotel group, should provide our guests with to further enhance your stay and exceed your expectations?',
       'If you have, which hotel was it and where?',
       'How would you describe your personality in 3 words?',
       'How would you describe your preferred aesthetic in 3 words?',
       'How many times have you travelled by plane during the pandemic?',
       'If you could describe yourself in one word, what would it be?',
       'How many times have you purchased flight tickets from the Airline XYZ?',
       'Social media consumption',
       'How would you describe the Global Hotel chain ABC in three adjectives?',
       'Hotel Chain ABC',
       'Describe in three words the most important factors to you which come to mind when thinking about booking  hotel accomodation.',
       'If you were to book hotel accomodation now that coronavirus has ended, do you think your main reason for booking hotel accomodation would have changed and if so, why? Please provide a few sentences.',
       'What measures do you find important to be introduced on plane? Please list in order of importance.',
       'Do you feel that your travelling behaviour has changed since the end of the pandemic? If yes, how so? If no, why not? ',
       'How would you feel, should those measures be implemented indefinitely? ',
       'Where are you currently based? (Please answer with reference to a country, eg: London, United Kingdom)',
       'What is your typical budget for international flights? (Answer in Pound Sterling)',
       'Please specify your frequently travelled destinations (eg: London, Dubai, LAX, etc. or more broadly: Finland, Sweden, etc.)',
       'What additional services do you prefer be made available to you in order to accommodate your concerns regarding the covid-19 virus? Feel free to be as descriptive as you like. (eg: availability of sanitizers on flights, etc.)',
       'Please specify other way', 'Please specify other', 'Room No',
       'Others, please specify',
       'Please let us know ways in which we can improve our services:',
       'Attachment',
       'Q20. What do you think could be the future of air travel in the foreseeable future post-COVID 19?',
       'Q22. If you chose "Yes" in the previous question, please input your contact e-mail address.',
       'Now that the COVID-19 pandemic has ended, how has your attitude towards visting a hotel changed?',
       'If you are a member of a hotel rewards program, please state why.',
       'If you replied no to the last question, what would change your mind such that you would consider booking directly with a hotel?',
       'If your response is yes please mention below',
       "What do you like most about the hotel's services?",
       'What made you choose ABC Hotel over a competitor?',
       'If you answered 3 or more in the previous question, please state the reasons why.',
       'Q9. If Yes for Q8, what was your main reason of repurchase? (Please put "-" if you answered No)',
       'Q18. In light of Covid-19, what recommendations would you give to airline companies in regard to extra health and safety precautions they could take?',
       'Which factor plays the biggest part in choosing a hotel? Why?',
       "The below image precisely explains why we are asking for your valuable inputs. In the below picture, everything is rainbow and roses from outsiders' viewpoint, but things do not seem that perfect when it comes to consumer viewpoint and consumer behaviour can lead to the ingrained pain points that they feel. Thus, looking at things from the consumers' perspective gives precious insights to understand and resolve consumer pain points.  ",
       'What is your usual length of stay in a hotel? (in days)',
       'What was your one of the best experiences while staying in a hotel? Also, do you think that this experience can be replicated looking at the precautions required post corona crisis? Why or Why not?',
       'Where are you from? (Major city)',
       'What measures would you like to be seen to be put in place on airlines to help stop the spread of Covid-19?',
       'Which social media outlet do you use the most?',
       'If yes, which one?',
       'Is there any thing we can do to make your experience delightful while using our services?',
       '7. What would you say are the basic components of a package deal for a business trip? (Please type your answer in the text box below. If this question does not apply to you please answer with "N/A")',
       '3. Please describe the advertisement you enjoyed most in the past month (Please type your response in the text box below)',
       '4. What is your country of residence? (Please type your answer in the text box below)',
       'How many times did you fly for personal reasons in the 12-month period before the start of the COVID-19 pandemic?',
       'How many times are you planning to fly in the next 12-month period for personal reasons?',
       'Do you have any preferred airlines?',
       'How many times did you fly for business reasons in the 12-month period before the start of the COVID-19 pandemic?',
       'Please list any other safety measures you would NOT like to see introduced at airports and/or on aircrafts.',
       'What do you think would be the likely inconveniences you as a passenger are likely to experience?',
       'In your opinion, what else should be changed now in your air travel experience, post the COVID-19 pandemic?',
       'What is your ethnicity?', 'Finding a flight ticket',
       'How can Airline XYZ support you with these new Covid 19-related needs?',
       'Survey Feedback:'
            ]

Ответы [ 5 ]

1 голос
/ 10 июля 2020

Вам понадобится df_merged['Section'].map({k: None for k in to_remove}).

Списки, наборы, dicts являются изменяемыми и не хешируемыми в качестве ключей в словаре, поэтому вы видите эту ошибку

0 голосов
/ 10 июля 2020

в дополнение к ответу @ C .Nivs, вы можете скомпилировать строку элементов, разделенных побитовым или статическим: | и использовать replace

import re 
import numpy as np
df = pd.DataFrame({'Section' : ['Name (Optional)', 'Date of Birth',
   'Please enter your phone number.','Leave me']})


print(df)
                           Section
0                  Name (Optional)
1                    Date of Birth
2  Please enter your phone number.
3                        Leave me
to_remove = ['Name (Optional)', 'Date of Birth',
   'Please enter your phone number.']

repl_string =  re.compile(r'|'.join(map(re.escape, to_remove)) + r'')
#re.compile('Name\\ \\(Optional\\)|Date\\ of\\ Birth|Please\\ enter\\ your\\ phone\\ number\\.')

df['Section'].replace(repl_string,np.nan,regex=True)

0              NaN
1              NaN
2              NaN
3         Leave me 
Name: Section, dtype: object
0 голосов
/ 10 июля 2020

Используйте pd.DataFrame.replace, поскольку @Mateus предлагает вот как вы можете это сделать

df_merged['Section'] = df_merged['Section'].replace(to_remove, [None]*len(to_remove))
0 голосов
/ 10 июля 2020

Вот как:

import pandas as pd

df = pd.DataFrame({'Col': ['value1', 'value2', 'value3', 'value4']})
lst = ['value1', 'value4']
df['Col'] = df['Col'].map(lambda v: 'in lst' if v in lst else v)

print(df)

Вывод:

      Col
0  in lst
1  value2
2  value3
3  in lst
0 голосов
/ 10 июля 2020

Для этого вы можете использовать dataframe.replace().

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...