Wie füge ich eine neue Spalte hinzu, ohne Warnung?
Verfasst: Mittwoch 19. Mai 2021, 15:17
Ich möchte eine Spalte zu einem Datensatz hinzufügen ('column1' und 'column2').
Soweit ich verstanden habe, sollte ich Option 1 wählen, aber egal welche Option ich versuche, ich bekomme die Warnung "SettingWithCopyWarning".
Ich möchte
1) csv lesen
2) Daten filtern und gefilterte Daten in eine neue Variable schreiben
3) Spalte mit konstantem Wert zu neuer Variable hinzufügen
4) ... später: einen Vionlin-Plot aus den Daten erstellen
Wo liegt mein Fehler?
Code:
Warning:
__________________________________
"Gerät:";"Title"
"Zeitpunkt";"0,5 µm (ft³) [Prt./ft³]";
04.05.2021 10:07:53;22672
04.05.2021 10:08:55;21512
04.05.2021 10:09:57;20972
04.05.2021 10:10:59;1918
04.05.2021 10:12:01;690
04.05.2021 10:13:03;588
04.05.2021 10:14:05;574
04.05.2021 10:15:07;351
__________________________________
Soweit ich verstanden habe, sollte ich Option 1 wählen, aber egal welche Option ich versuche, ich bekomme die Warnung "SettingWithCopyWarning".
Ich möchte
1) csv lesen
2) Daten filtern und gefilterte Daten in eine neue Variable schreiben
3) Spalte mit konstantem Wert zu neuer Variable hinzufügen
4) ... später: einen Vionlin-Plot aus den Daten erstellen
Wo liegt mein Fehler?
Code:
Code: Alles auswählen
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from matplotlib import pyplot as plt
import seaborn as sns
data_1 = pd.read_csv('minimal_data_1.csv', delimiter= ';', decimal=',', usecols=[0,1],skiprows=[0])
data_1_1 = pd.DataFrame()
data_1_1['Time'] = pd.to_datetime(data_1['Zeitpunkt'])
data_1_1['0,5 µm'] = data_1['0,5 µm (ft³) [Prt./ft³]']
data_1_2 = pd.DataFrame()
data_1_2 = data_1_1.loc[(data_1_1['Time']>'2021-04-05 10:10:00') & (data_1_1['Time']<'2021-04-05 10:14:00')]
#option1:
data_1_2.loc[:,'column1'] = '0.45'
#option2:
data_1_2['column2'] = '0.45'
print(data_1_2.head())
Warning:
minimal_data_1.csv:
C:\...\Python\Python37\site-packages\pandas\core\indexing.py:845: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/s ... sus-a-copy
self.obj[key] = _infer_fill_value(value)
C:\...\Python\Python37\site-packages\pandas\core\indexing.py:966: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
__________________________________
"Gerät:";"Title"
"Zeitpunkt";"0,5 µm (ft³) [Prt./ft³]";
04.05.2021 10:07:53;22672
04.05.2021 10:08:55;21512
04.05.2021 10:09:57;20972
04.05.2021 10:10:59;1918
04.05.2021 10:12:01;690
04.05.2021 10:13:03;588
04.05.2021 10:14:05;574
04.05.2021 10:15:07;351
__________________________________