Replace a pattern in column of a csv file using Python

126
April 14, 2022, at 9:40 PM

I am looking for some help with editing CSV in python. I have a file in below format

Account InstanceId InstanceName InstanceType Platform Status AvailabilityZone PrivateIpAddress publicIpAdress Owner
12345678901 i-0abcdef1234567890 Test123 t3a.micro windows stopped ap-southeast-2a 10.0.0.100 arn:aws:sts::12345678901:user/test-user
98765432109 i-0987654321abcdefg test345 t2.nano Linux running ap-southeast-2b 10.0.0.200 99.99.99.100 arn:aws:sts::98765432109:assumed-role/testrole/user@example.com
98765432109 i-abcdefghij01234899 test987 t2.nano windows running ap-southeast-2b 10.0.0.201

I would like to edit this file in place and and have the resulting file in below format

Account InstanceId InstanceName InstanceType Platform Status AvailabilityZone PrivateIpAddress publicIpAdress Owner
12345678901 i-0abcdef1234567890 Test123 t3a.micro windows stopped ap-southeast-2a 10.0.0.100 test-user
98765432109 i-0987654321abcdefg test345 t2.nano Linux running ap-southeast-2b 10.0.0.200 99.99.99.100 user@example.com
98765432109 i-abcdefghij01234899 test987 t2.nano windows running ap-southeast-2b 10.0.0.201

I tried a few things with no luck and would really appreciate if you can help me with this. Thanks in advance.

Answer 1

Use:

df['Owner'] = df['Owner'].str.split('/').str[-1]

Demonstration:

df = pd.DataFrame({'Owner': ['arn:aws:sts::12345678901:user/test-user']})
df['Owner'].str.split('/').str[-1]

Output:

Rent Charter Buses Company
READ ALSO
Making animation on matplotlib of graph with Networkx efficiently

Making animation on matplotlib of graph with Networkx efficiently

I am trying to animate a graph whose edges widths and color change over timeMy code works, but it is extremely slow

120
Random sampling in python3

Random sampling in python3

Suppose I have two lists A = [1,2,3,4] and B=[a,b,c,d]I have to do random sampling such that my new list could look like this C= [1 or a , 2 or b , 3 or c , 4 or d], there is also a constrain that that the list C should have 50 % data from both A and B

89
Autoclicker that clicks left click and uses left click as the hot key

Autoclicker that clicks left click and uses left click as the hot key

This seems really simple but, I realized the issue is that when pyautogui left clicks the key state is reset to up even though I may still be holding down left click

96
Breaking down np.where with nested for-loop and if/else statement

Breaking down np.where with nested for-loop and if/else statement

Write Python code equivalent to the following:

120