1
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('/OneDrive/Desktop/UNI/Angew_SYS1/Projekt/2012_gwr_sprengelerg.csv', sep = ';', encoding='cp1252')

I have this big dataframe and i want to delete the index (0-2991).

with df1.reset_index(drop=True, inplace=True) nothing changes and with df.style.hide_index() it removes the index but in the next step I where I use

dataframe = pd.DataFrame(columns=['stimm', "stimm2"])

dataframe["stimm"] = df.loc[df['ptname'] == 'SPÖ', 'stimmen']

dataframe["stimm2"] = df.loc[df['ptname'] == 'FPÖ', 'stimmen']

print(dataframe)

AttributeError: 'Styler' object has no attribute 'loc'

Does anyone know what to do?

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
Andi
  • 11
  • 1
  • 1
    What are you trying to achieve here exactly? Just so you know, a Pandas Dataframe will always have an `index` by default. So, even if you delete index values, pandas will create a default index. – Mayank Porwal Dec 31 '20 at 13:17
  • Does this answer your question? [How to drop a list of rows from Pandas dataframe?](https://stackoverflow.com/questions/14661701/how-to-drop-a-list-of-rows-from-pandas-dataframe) – Gigioz Dec 31 '20 at 14:51

1 Answers1

0
dataframe["stimm"] = df.loc[df['ptname'] == 'SPÖ', 'stimmen']
dataframe["stimm2"] = df.loc[df['ptname'] == 'FPÖ', 'stimmen']

print(dataframe)

in my new dataframe I have columns "stimm" and "stimm2". The problem is that in "stimm2" are NaNs, because in "stimm" it took already all the index from this

dataframe["stimm"] = df.loc[df['ptname'] == 'SPÖ', 'stimmen']

But there isnt the "ptname" "FPÖ", in those index. So it gives me out the NaNs

sophocles
  • 13,593
  • 3
  • 14
  • 33
Andi
  • 11
  • 1