Saya memiliki file bingkai data di bawah ini dengan dua kolom "pesan" dan "nama".

data = [['Extension of Time to Comply with Mandatory Disclosure', ['Order']], 
    ['I electronically filed the foregoing document', ['Hearing', 'Urgent']], 
    ['Said oral examination', ['Pleading', 'My Filings']],
    ['In the event of this cause', ['Discovery', 'My Filings']],
   ['A true and correct copy', ['My Filings']],
   ['A credible explanation of human error', ['Urgent']],
   ['Oral examination error', ['Urgent', 'My Filings']]]

df = pd.DataFrame(data, columns = ['Message', 'name'])

Saya harus melakukan dua hal menggunakan panda.

  1. Dalam kolom "nama" jika hanya ada "mendesak" atau "pengajuan saya" atau keduanya tetapi tidak ada yang lain dalam daftar kemudian tambahkan "uncategorized" dalam daftar. Seperti gambar di bawah ini. masukkan deskripsi gambar di sini

  2. Setelah itu jika ada "'mendesak']" di kolom nama, pindahkan di kolom berikutnya yang disebut "urgent_flag" lain tulis "['unflag_urgent']". Atau jika ada "['pengajuanku']", pindahkan di kolom berikutnya yang disebut "myfilings_flag" lain tulis "['unflag_myfilings']". Atau jika ada "['mendesak', 'pengajuan saya']", lalu pindahkan "['mendesak']" di kolom "Urgent_Flag" dan "['Filings My']" di kolom "myfilings_flag". Seperti gambar di bawah ini. masukkan deskripsi gambar di sini

Bagaimana saya bisa melakukan ini di Python?

1
Ekanshu 3 April 2021, 17:50

2 jawaban

Jawaban Terbaik

Anda dapat mencapai ini menggunakan fungsi atau Anda dapat memutuskan untuk menulis beberapa pernyataan Terapkan.

Jika Anda ingin menulis 4 baris kode untuk menyelesaikan ini, inilah cara melakukannya.

df['new_name'] = df.name.apply(lambda x: ['Unauthorized'] if all(y in ('Urgent', 'My Filings') for y in x) else x)
df['new_name'] = df.new_name.apply(lambda x: [y for y in x if y not in ('Urgent', 'My Filings')])
df['urgent_flag'] = df.name.apply(lambda x: ['Urgent'] if any(y in 'Urgent' for y in x) else ['Unflag_urgent'])
df['myfilings_flag'] = df.name.apply(lambda x: ['My Filings'] if any(y in 'My Filings' for y in x) else ['Unflag_myfilings'])

print (df.iloc[:,1:]) #ignoring first column as its irrelevant for this problem statement

Output akan:

                      name        new_name      urgent_flag      myfilings_flag
0                  [Order]         [Order]  [Unflag_urgent]  [Unflag_myfilings]
1        [Hearing, Urgent]       [Hearing]         [Urgent]  [Unflag_myfilings]
2   [Pleading, My Filings]      [Pleading]  [Unflag_urgent]        [My Filings]
3  [Discovery, My Filings]     [Discovery]  [Unflag_urgent]        [My Filings]
4             [My Filings]  [Unauthorized]  [Unflag_urgent]        [My Filings]
5                 [Urgent]  [Unauthorized]         [Urgent]  [Unflag_myfilings]
6     [Urgent, My Filings]  [Unauthorized]         [Urgent]        [My Filings]
1
Joe Ferndz 3 April 2021, 17:04

Solusi lain:

def fn(x):
    a, b, c = x.copy(), [], []

    if (
        ("Urgent" in a and len(a) == 1)
        or ("My Filings" in a and len(a) == 1)
        or (("My Filings" in a) and ("Urgent" in a) and len(a) == 2)
    ):
        a.append("Uncategorized")

    if "Urgent" in x:
        a.remove("Urgent")
        b.append("Urgent")
    else:
        b.append("Unflag_urgent")

    if "My Filings" in x:
        a.remove("My Filings")
        c.append("My Filings")
    else:
        c.append("Unflag_myfilings")

    return a, b, c


df[["name", "urgent_flag", "myfilling_flag"]] = df["name"].apply(
    lambda x: pd.Series(fn(x))
)
print(df)

Cetakan:

                                             Message             name      urgent_flag      myfilling_flag
0  Extension of Time to Comply with Mandatory Dis...          [Order]  [Unflag_urgent]  [Unflag_myfilings]
1      I electronically filed the foregoing document        [Hearing]         [Urgent]  [Unflag_myfilings]
2                              Said oral examination       [Pleading]  [Unflag_urgent]        [My Filings]
3                         In the event of this cause      [Discovery]  [Unflag_urgent]        [My Filings]
4                            A true and correct copy  [Uncategorized]  [Unflag_urgent]        [My Filings]
5              A credible explanation of human error  [Uncategorized]         [Urgent]  [Unflag_myfilings]
6                             Oral examination error  [Uncategorized]         [Urgent]        [My Filings]
1
Andrej Kesely 3 April 2021, 15:13