I read "data.csv" file and has 2 columns.
For "code" columns, I would like to slice the value string and put back only last 2 digit(alphabet) of code into columns.
For each rows, if either "username" and "code" value is missing, I would like to remove the row from the list.
import pandas as pd
df.to_csv("data.csv")
df = pd.read_csv("data.csv", usecols = ['username, 'code'])
print(dt)
What I have now
username code
Chris AB-12-DF
Jake
Rebecca JH-34-TY
LK-49-RT
Expected Output
username code
Chris DF
Rebecca TY
CodePudding user response:
I'm assuming the empty cells are ""
strings:
df = df[~df.eq("").any(axis=1)]
df["code"] = df["code"].str.rsplit("-", n=1).str[-1]
print(df)
Prints:
username code
0 Chris DF
2 Rebecca TY