How to treat columns with the same name in csv file

Asked

Viewed 88 times

0

I have the following problem, I received a database in csv file and I need to analyze this data, but it has a lot of columns with the same name and I wanted to know if you have any way to treat these columns without changing the file itself, just manipulating the dataframe.

Code to generate dataframe:

import pandas as pd
base = pd.read_csv('Final_behavior_study_results.csv')

How the columns appear:

# | Describe your experience | what is the feeling in this photo? | what is the feeling in this photo? | what is the feeling in this photo? | Start Date | Submite Date | Network ID
  • You can rename all columns by passing a list with the new names, for example: df.columns = ["col1", "col2"]

  • Thiago, good morning! If they have the same name it may be interesting to pivot your DF. If you want to make the csv file available, we can get a better idea of what it is. Hug!

1 answer

0


Very strange. Pandas already treat it.

Behold here

create a file teste.csv containing

a,b,a
1,2,3

Then use in Python

>>> import pandas as pd
>>> df = pd.read_csv(open("teste.csv", "r"))
>>> df
   a  b  a.1
0  1  2    3

See that the second column a received a .1 getting a.1 and in csv there was only a

Check for extra spaces in the field name.

I hope it helps

Browser other questions tagged

You are not signed in. Login or sign up in order to post.