简体   繁体   English

如何将字符串添加到pandas dataframe列系列中的每个偶数行?

[英]How to add a string to every even row in a pandas dataframe column series?

I am new to pandas. 我是熊猫新手。

I want to add a new column to a pandas dataframe df and assign "Start" to every odd row and "Stop" to every even row. 我想在pandas dataframe df添加一个新列,并为每个奇数行分配“Start”,为每个偶数行分配“Stop”。

However, when I do df.iloc[1::2, :] = "Start" , I am inserting a new row at every 2nd position with the "Start" string in every column. 但是,当我执行df.iloc[1::2, :] = "Start" ,我在每个第二个位置插入一个新行,每列都有“Start”字符串。

I know that in this case, pandas doesn't know in which column to put the "Start"-string. 我知道在这种情况下,pandas不知道在哪个列中放置“Start”-string。

However, I couldn't figure out the correct syntax. 但是,我无法弄清楚正确的语法。

Here's my solution - Haven't figured out the optimization part but given a fairly large dataset this should handle it quite well - 这是我的解决方案 - 没有弄清楚优化部分,但是给出了一个相当大的数据集,这应该可以很好地处理它 -

import pandas as pd

df = pd.read_csv('temp.csv')

df['New_Col'] = "Start"

df.loc[1::2,"New_Col"] = "Stop"

print df['New_Col']

Output - 输出 -

0      Start
1       Stop
2      Start
3       Stop
4      Start
5       Stop
6      Start
7       Stop
8      Start
9       Stop
10     Start
11      Stop
12     Start
13      Stop
14     Start
15      Stop
16     Start
17      Stop
18     Start
19      Stop
20     Start
21      Stop
22     Start
23      Stop
24     Start
25      Stop
26     Start
27      Stop
28     Start
29      Stop
       ...  
116    Start
117     Stop
118    Start
119     Stop
120    Start
121     Stop
122    Start
123     Stop
124    Start
125     Stop
126    Start
127     Stop
128    Start
129     Stop
130    Start
131     Stop
132    Start
133     Stop
134    Start
135     Stop
136    Start
137     Stop
138    Start
139     Stop
140    Start
141     Stop
142    Start
143     Stop
144    Start
145     Stop
Name: New_Col, dtype: object

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为每个唯一列值 pandas dataframe 添加行系列? - How to add row series per unique column value pandas dataframe? 如何将Series添加为DataFrame(Pandas)中的列? - How to add Series as a column in a DataFrame (Pandas)? 熊猫向数据框列添加系列 - Pandas add a series to dataframe column 如何检查PANDAS DataFrame列中是否包含一系列字符串,并将该字符串分配为行中的新列? - How to check if a series of strings is contained in a PANDAS DataFrame columns and assign that string as a new column in the row? 将 Pandas 系列作为一行添加到 Pandas DataFrame - Add Pandas Series as a Row to Pandas DataFrame 在 pandas dataframe 中为另一个日期框列中的每个日期添加一行 - Add a row in pandas dataframe for every date in another dateframe column 如何向 pandas dataframe 添加一列,该列在一个范围内具有最高值但将其应用于每一行? - How do I add a column to a pandas dataframe which has the highest value in a range but applying it to every row? Pandas,如何将系列添加到 DataFrame 列,其中系列索引与 DataFrame 列匹配? - Pandas, how to add Series to DataFrame column, where series index matches a DataFrame column? 如何通过在 Pandas 时间序列数据框中搜索数据来添加新列 - How to add a new column by searching for data in a Pandas time series dataframe 有效地将单行添加到Pandas Series或DataFrame - Efficiently add single row to Pandas Series or DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM