简体   繁体   中英

Appending to a dataframe cell with pandas?

I'm working on a code that assigns tags to people based on data in certain columns. I'm using a pandas dataframe. I had no problem populating the tag column with one initial value, but I can't figure out how to append to the initial value if a person should have more than one tag.

The dataframe is treating each cell like a string, pretty sure I want it to be a list.

df["Shopify Tags"] = ''
df.set_index(ID, inplace=True)

i = 0

for index, row in df.iterrows():

     if "Medical" in df.iloc[i,2]:

          df.iloc[i,4] = "#Medical"


     if "40" in df.iloc[i,2]:

         df.iloc[i,4].append('#Discount40')


 i+=1

I want the Shopify Tags column to eventually look like #Medical, #Discount40, #OtherTags in each row

This is my first question on SO :)

There are 2 points worth noting for your problem:

  1. Holding lists in a dataframe is inefficient and not recommended. This is because they are stored via pointers rather than in contiguous memory blocks. This means vectorised computations are not possible.
  2. You should only iterate rows in a dataframe as a last resort. Pandas specialises in vectorised computations. Even for non-vectorised operations there are methods which avoid explicit for loops.

Having noted these points, below is one solution.

# example dataframe
df = pd.DataFrame({'col1': 1,
                   'col2': ['Medical 1234', 'Medical 40 Something',
                            '40 something', 'Nothing'],
                   'col3': 3})

# define function which creates a list from a dictionary mapping
def lister(x):
    mapping = {'Medical': '#Medical', '40': '#Discount40'}
    return [v for k, v in mapping.items() if k in x]

# apply function to series
df['col4'] = df['col2'].apply(lister)

print(df)

   col1                  col2  col3                     col4
0     1          Medical 1234     3               [#Medical]
1     1  Medical 40 Something     3  [#Medical, #Discount40]
2     1          40 something     3            [#Discount40]
3     1               Nothing     3        

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM