Python 使用panda dataframe列值并粘贴到下一行

Python 使用panda dataframe列值并粘贴到下一行,python,pandas,dataframe,Python,Pandas,Dataframe,我对Python中的Panda dataframe非常陌生。我正在编写一个csv文件结构如下所示的代码: Id, Title, Body, Tags, Date 1, First question, My first question, robot Python, 2015 2, Second question, My second question, C++ Python, 2015 3, Third question, My third question, Selenium, 2016 4,

我对Python中的Panda dataframe非常陌生。我正在编写一个csv文件结构如下所示的代码:

Id, Title, Body, Tags, Date
1, First question, My first question, robot Python, 2015
2, Second question, My second question, C++ Python, 2015
3, Third question, My third question, Selenium, 2016
4, Fourth question, My fourth question, Java C++, 2016
Id, Title, Body, Tags, Date
1, First question, My first question, robot, 2015
2, First question, My first question, Python, 2015
3, Second question, My second question, C++, 2015
4, Second question, My second question, Python, 2015
.......
我已经使用Panda库将这个CSV导出到我的python代码中

我正在尝试获得如下所示的数据帧:

Id, Title, Body, Tags, Date
1, First question, My first question, robot Python, 2015
2, Second question, My second question, C++ Python, 2015
3, Third question, My third question, Selenium, 2016
4, Fourth question, My fourth question, Java C++, 2016
Id, Title, Body, Tags, Date
1, First question, My first question, robot, 2015
2, First question, My first question, Python, 2015
3, Second question, My second question, C++, 2015
4, Second question, My second question, Python, 2015
.......

请告诉我是否有任何合适的方法来实现这一点

最好的做法是提供您正在尝试执行的操作的完整代码,以便我们能够完全帮助您

我认为你试图做的只是简单地替换一些值。您可以使用此结构

df['column name'] = df['column name'].replace(['old value'],'new value')
以你为例

df['Title'] = df['Title'].replace({'Second Question': 'First Question',
                                   'Second Question' : 'Third Question"}),
                                    inplace = True)

等等等等。

你可以这样做:

df = df.drop(["Id"], axis=1)
df2 = pd.DataFrame(columns=df.columns)
for index, row in df.iterrows():
    aux = row
    for tag in row["Tags"].split():
        aux["Tags"] = tag
        df2 = df2.append(aux)
df2.reset_index(drop=True)
其中df是您的数据帧,df2是更新的数据帧。您迭代数据帧df的每一行,并将“Tags”值拆分为尽可能多的标记(在您的示例中,最大值为2,但我认为您可以有更多)。然后将带有每个单独标记的行附加到新的数据帧df2。 (我删除id并重置索引,因为它保留原始索引值)


他试图根据标签值复制列,而不是重命名标题。。。