0
$\begingroup$

I have a CSV file that looks like below with columns labeled below data, and I'm have trouble trying to retrieve said data with pandas. I'm only able to address the columns at the top with the specific rows within that parameter with using df["crit_rating"][0:3], however i'm unable to use df["agility] to address as a seperate column. Is there a way to do this without having the make another CSV file or reformatting the file?

crit_rating, DPS, DPS-Error
-1250, 4439.51, 11.2203
-1225, 4438.28, 11.3488
-1200, 4439.3, 11.2504
-1175, 4442.01, 13.0366
 
agility, cDPS, cDPS-Error
-1250, 1400.14, 3.62795
-1225, 1473.09, 4.28675
-1200, 1547.47, 4.47641
-1175, 1620.25, 4.63525

Where I'd like it to be:

agility, DPS, DPS-Error, crit_rating, cDPS, cDPS-Error
-1250, 1400.14, 3.62795, -1250, 4439.51, 11.2203
-1225, 1473.09, 4.28675, -1225, 4438.28, 11.3488
-1200, 1547.47, 4.47641, -1200, 4439.3, 11.2504
-1175, 1620.25, 4.63525, -1175, 4442.01, 13.0366   
$\endgroup$
1
  • $\begingroup$ Can you share what your input data file looks like? If you are able to access the crit_rating column I would expect that doing the same for agility should also work. $\endgroup$
    – Oxbowerce
    Commented Mar 15, 2021 at 8:18

1 Answer 1

1
$\begingroup$

You can use pd.read_csv funcation with perameters of nrows and skiprows to read two different data frame and then concat them horizontally

import pandas as pd
df = pd.read_csv('your csv file path',nrows=4)
df1 = pd.read_csv('your csv file path',skiprows=5)
df = pd.concat([df, df1], axis = 1, sort = False)
print(df)

__output__

   crit_rating      DPS   DPS-Error  agility     cDPS   cDPS-Error
0        -1250  4439.51     11.2203    -1250  1400.14      3.62795
1        -1225  4438.28     11.3488    -1225  1473.09      4.28675
2        -1200  4439.30     11.2504    -1200  1547.47      4.47641
3        -1175  4442.01     13.0366    -1175  1620.25      4.63525

same you can implement with a single line of code i.e.

df = pd.concat([pd.read_csv('your csv file path',nrows=4), pd.read_csv('your csv file path',skiprows=5)], axis = 1, sort = False)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.