简体   繁体   中英

Python pandas dataframe: Looping through each row, if condition is true, update column

I have a CSV that has a list of URLs that I need to see if they exist in other columns. I have the following code that loops through each row of the column called "URLS" that checks to see if this exists on another specific column. If this does, then I need to add a string to a specific column for the row. I have it functioning, but I'm not sure how I update the column for the row? I'm reading through the docs and I'm thinking I might be over thinking a bit on this.

import pandas as pd

# Import CSV
data = pd.read_csv(r'URL_export.csv')

# Looping through the URLS of this column
df = pd.DataFrame(data, columns = ['URL', 'Exists'])

# Checking if URLs exist in this row
v = pd.DataFrame(data, columns = ['Check'])

for row in df.itertuples():
    if row.URL in v.Check.values:
        print(row)
        # Add string "Yes" under column name "Exists" for this row
import pandas as pd

df = pd.DataFrame({
    'URL': ['a', 'b', 'c' ,'d', 'e', 'f'],
    'Exists': ['','','', '', '', '']
})

v = pd.DataFrame({
    'Check': ['a', 'c', 'e']
})

df['Exists'] = df['URL'].apply(lambda x: 'Yes' if x in v['Check'].values else 'No')

Output:

输出

If it's needed just assign "Yes" (without "No"):

df['Exists'] = df['Exists'] + ' ' + df['URL'].apply(lambda x: 'Yes' if x in v['Check'].values else '')

If column "Exists" already contains a value and you need to append "Yes" to it:

df['Exists'] = df['Exists'] + ' ' + df['URL'].apply(lambda x: 'Yes' if x in v['Check'].values else '')

It's probably better to use booleans, instead of the strings 'Yes' and 'No' .

This also helps simplify the code:

import pandas as pd

df_1 = pd.DataFrame({'URL': ['a', 'b', 'd', 'c', 'e', 'f']})
print(df_1, end='\n\n')

df_2 = pd.DataFrame({'Check': ['a', 'c', 'e']})
print(df_2, end='\n\n')

df_1['Exists'] = df_1['URL'].isin(df_2['Check'])
print(df_1)

Output:

  URL
0   a
1   b
2   d
3   c
4   e
5   f

  Check
0     a
1     c
2     e

  URL  Exists
0   a    True
1   b   False
2   d   False
3   c    True
4   e    True
5   f   False

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