Python Forum
[Solved] df.loc: write data in certain rows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] df.loc: write data in certain rows
#1
Lightbulb 
I import a text file and would like to write a comment in certain rows, which I access with df.loc.
However, I get the error "# When setting, missing keys are not allowed, even with .loc:".
Could someone tell me, how to avaid it? I have checked the documentation, where a similar way worked out .

import pandas as pd
import numpy as np

df = pd.read_csv('2021-06-15_data.txt', delimiter= '\t',parse_dates=[[0, 1]], header=None, names=["Date","Time","Channel","time","0.3","0.5","1.0","3.0","5.0","10.0"])
df['Date_Time'] = df['Date_Time'] + pd.Timedelta(days = 365*20)

# print types works fine
print(df.head(5)) 
df.dtypes

# iloc works fine
print("iloc") 
df.iloc[5:7]

# loc works fine
print("loc")
df.loc[(df['Date_Time']  >= '2021-06-10 09:51:04')&(df['Date_Time']  < '2021-06-10 09:51:26')] 

# create new column 
df['Comment']=""

# write data in column 'Comment', only for certain rows creates KeyError
df.loc[(df['Date_Time']  >= '2021-06-10 09:51:04')&(df['Date_Time']  < '2021-06-10 09:51:26'),df['Comment']]="zero"
2021-06-15_data.txt:
15.06.2001	09:50:42	1	10	5	4	4	1	1	1
15.06.2001	09:50:53	1	10	0	0	0	0	0	0
15.06.2001	09:51:04	1	10	0	0	0	0	0	0
15.06.2001	09:51:15	1	10	0	0	0	0	0	0
15.06.2001	09:51:26	1	10	0	0	0	0	0	0
15.06.2001	09:51:37	1	10	0	0	0	0	0	0
15.06.2001	09:51:48	1	10	0	0	0	0	0	0
15.06.2001	09:51:59	1	10	0	0	0	0	0	0
15.06.2001	09:52:10	1	10	0	0	0	0	0	0
15.06.2001	09:52:21	1	10	0	0	0	0	0	0

Attached Files

.txt   2021-06-15_data.txt (Size: 14.02 KB / Downloads: 290)
Reply
#2
I found the solution:

# write data in column 'Comment', only for certain rows creates KeyError
df.loc[(df['Date_Time']  >= '2021-06-10 09:50:31')&(df['Date_Time']  < '2021-06-10 09:51:00'),'Comment']="zero" 
df
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how do you style data frame that has empty rows. gsaray101 0 498 Sep-08-2023, 05:20 PM
Last Post: gsaray101
  (Python) Pulling data from UA Google Analytics with more than 100k rows into csv. Stockers 0 1,170 Dec-19-2022, 11:11 PM
Last Post: Stockers
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,798 Dec-12-2022, 08:22 PM
Last Post: jh67
  Write sql data or CSV Data into parquet file mg24 2 2,355 Sep-26-2022, 08:21 AM
Last Post: ibreeden
  Is this possible to write a script for checking data from website? WanW 2 1,081 Jun-02-2022, 02:31 AM
Last Post: Larz60+
  Write and read back data Aggie64 6 1,811 Apr-18-2022, 03:23 PM
Last Post: bowlofred
  [SOLVED] Concat data from dictionary? Winfried 4 1,666 Mar-30-2022, 02:55 PM
Last Post: Winfried
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,599 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  rows from sql query need to write to a file as columns sjcsvatt 6 2,331 Oct-09-2021, 12:45 AM
Last Post: snippsat
  Iterating Through Data Frame Rows JoeDainton123 2 2,860 Aug-09-2021, 07:01 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020