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: 55)
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
  Iterating Through Data Frame Rows JoeDainton123 3 1,007 Aug-09-2021, 07:01 AM
Last Post: Pedroski55
  [Solved] Plotting data from txt file Laplace12 1 308 Jul-06-2021, 07:14 AM
Last Post: Laplace12
  [Solved] Using readlines to read data file and sum columns Laplace12 4 486 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 468 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 1,769 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  Pandas: how to split one row of data to multiple rows and columns in Python GerardMoussendo 4 1,690 Feb-22-2021, 06:51 PM
Last Post: eddywinch82
  How to filter out Column data From Multiple rows data? firaki12345 10 1,307 Feb-06-2021, 04:54 AM
Last Post: buran
  write mariadb table rows query to each file? shams 1 665 Feb-02-2021, 04:10 PM
Last Post: buran
  Moving Rows From Different Data Frames JoeDainton123 1 1,158 Aug-06-2020, 05:19 AM
Last Post: scidam
  Extracting Rows From Data Frame and Understanding The Code JoeDainton123 0 585 Aug-03-2020, 04:08 PM
Last Post: JoeDainton123

Forum Jump:

User Panel Messages

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