1

I am reading a .csv file into a pandas dataframe and have the output like this;

Name                DOB         Phone           Address           Unnamed: 4      Unnammed: 5     
WERNER,BRADLEY D    11/14/1962  563-921-9775    518 E 1st St N
THOMPSON,LARRY E    1/22/1950   235-660-0613    516 Clark St Box 
CORNELIO,LESA L     11/11/1977                                    479-308-3957    208 S Sorenson Ave 
SCHUBERT,BONITA J   6/29/1966   756-364-8059    120 S RICE ST

From above, I want to find the values that are present in Unnamed:<number> columns as move two columns to the left (into Phone and Address columns).

I tried this,

for i in df.columns:
    if i.startswith("Unnamed"):
        column_val = df[i].dropna()
        print(column_val)

Here I get the output,

> 479-308-3957
  Name: Unnamed: 4, dtype: object 

> 208 S Sorenson Ave
  Name: Unnamed: 5, dtype: object

Now, I get the values needed, but now, I want to move it two columns to the left. Output would be,

 Name               DOB         Phone           Address               
 WERNER,BRADLEY D   11/14/1962  563-921-9775    518 E 1st St N
 THOMPSON,LARRY E   1/22/1950   235-660-0613    516 Clark St Box 
 CORNELIO,LESA L     11/11/1977 479-308-3957    208 S Sorenson Ave 
 SCHUBERT,BONITA J  6/29/1966   756-364-8059    120 S RICE ST

Also, if I have a big data frame with multiple values under Unnamed columns will my method to find out the values be efficient?

Any help would be great and an efficient way will be much awesome.

user9431057
  • 1,203
  • 1
  • 14
  • 28
  • You'd be better off fixing the code that reads your data. – cs95 Jun 13 '19 at 19:12
  • @cs95 That is what I found online when I was searching for reading corrupted data from `.csv` (i.e. `pd.read_csv(..., error_bad_lines = False)`). I found a way around based on (https://stackoverflow.com/a/38251624/6626093) answer and read in a file, save back as `.CSV` and read saved file into pandas. The reason is I don't want to lose data and I want to automate this. Hope this helps to explain my situation. – user9431057 Jun 13 '19 at 19:19

0 Answers0