Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help on String variable
#4
Hi Yoriz

little updates from my side whatever I have done.

**********************
import pandas as pd
import numpy as np
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

csv1="multiple_responses.csv"
df1 = pd.read_csv(csv1, index_col='id' , na_values = [' '] , low_memory=False)

method_names = ['female_condoms', 'emergency', 'male_condoms', 'pill', 'injectables', 'iud', 'male_sterilization', 'female_sterilization']
for method in method_names:
print(method)

for method in method_names:
df1[method]=df1["methods_discussed"].str.contains(pat = method)
df1.head(10)

output
id | methods_discussed | female_condoms | emergency | male_condoms | pill | injectables | iud | male_sterilization | female_sterilization
1 | emergency | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE
2 | female_sterilization | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE
3 | male_sterilization | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE
4 | iud | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE
5 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN
6 | injectables male_condoms | FALSE | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | FALSE
7 | male_condoms | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE
8 | female_sterilization male_sterilization | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE
9 | injectables | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE
10 | iud male_condoms | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE

Problem description
I used CSV file (link of CSV file is https://github.com/pandas-dev/pandas/fil...ponses.zip)
which contains two columns "id" and "methods_discussed". After running above code the ouput shown is wrong as at index [2] column male_sterilization shows TRUE (I have made it bold and italic. It should be FALSE as "methods_discussed" contains only female_sterilization.

Expected Output
id | methods_discussed | female_condoms | emergency | male_condoms | pill | injectables | iud | male_sterilization | female_sterilization
1 | emergency | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE
2 | female_sterilization | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE
3 | male_sterilization | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE
4 | iud | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE
5 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN
6 | injectables male_condoms | FALSE | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | FALSE
7 | male_condoms | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE
8 | female_sterilization male_sterilization | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE
9 | injectables | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE
10 | iud male_condoms | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE

I have also used str.match but it did not work for me.
Any idea if I don't want to generate values if methods_discussed contains NaN.

Thanks

Ashish
Reply


Messages In This Thread
Help on String variable - by ashishstats - Aug-04-2019, 11:19 AM
RE: Help on String variable - by Yoriz - Aug-04-2019, 12:34 PM
RE: Help on String variable - by ashishstats - Aug-05-2019, 09:39 AM
RE: Help on String variable - by ashishstats - Aug-13-2019, 08:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing String Variable with a new String Name kevv11 2 927 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Need help on how to include single quotes on data of variable string hani_hms 5 2,448 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  python r string for variable mg24 3 3,318 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  USE string data as a variable NAME rokorps 1 1,074 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,631 Aug-23-2022, 01:15 PM
Last Post: louries
  Remove a space between a string and variable in print sie 5 2,003 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Split string using variable found in a list japo85 2 1,454 Jul-11-2022, 08:52 AM
Last Post: japo85
  Can you print a string variable to printer hammer 2 2,136 Apr-30-2022, 11:48 PM
Last Post: hammer
Question How to convert string to variable? chatguy 5 2,842 Apr-12-2022, 08:31 PM
Last Post: buran
  I want to search a variable for a string D90 lostbit 3 2,778 Mar-31-2021, 07:14 PM
Last Post: lostbit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020