Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用两个文件更改日期格式_Python_Pandas_Csv_Dataframe - Fatal编程技术网

Python 如何使用两个文件更改日期格式

Python 如何使用两个文件更改日期格式,python,pandas,csv,dataframe,Python,Pandas,Csv,Dataframe,我有两张桌子,想用熊猫把它们换成一张看起来像这样的桌子。订单必须相同,日期必须完全相同 我的table1.csv Tweet, Month, Day, Year Hello World, 6, 2, 2013 I want ice-cream!, 7, 23, 2013 Friends will be friends, 9, 30, 2017 Done with school, 12, 12, 2017 我的table2.csv Month, Day, Year, Hour, Tweet Ja

我有两张桌子,想用熊猫把它们换成一张看起来像这样的桌子。订单必须相同,日期必须完全相同

我的table1.csv

Tweet, Month, Day, Year
Hello World, 6, 2, 2013
I want ice-cream!, 7, 23, 2013
Friends will be friends, 9, 30, 2017
Done with school, 12, 12, 2017
我的table2.csv

Month, Day, Year, Hour, Tweet
January, 2, 2015, 12, Happy New Year
March, 21, 2016, 7, Today is my final
May, 30, 2017, 23, Summer is about to begin
July, 15, 2018, 11, Ocean is still cold
这就是我到目前为止所做的:

import numpy as np

import pandas as pd

from datetime import *


df1=pd.read_csv('data1.csv', index_col=False, header=0)

df2=pd.read_csv('data2.csv', index_col=False, header=0)

#creating Date column from Day,Month and Year columns
df1['Date']= df1.apply(lambda x:datetime.strptime("{0} {1} {2}"
                .format(x['Year'],x['Month'], x['Day']), "%Y %m %d"),axis=1)



df2['Date']= df2.apply(lambda x:datetime.strptime("{0} {1} {2}"
                .format(x['Year'],x['Month'], x['Day']), "%Y %B %d"),axis=1)

#Selecting only desired columns
df1=df1[['Date','Tweet']]
df2=df2[['Date','Tweet']]

#combining both data frames
combine=df1.append(df2)

#Sort the data frame based on Date column.
combine.sort_values(by='Date', ascending=False, inplace=True)

#convert date to required format
combine['Date'] = combine['Date'].dt.strftime('%m-%b-%Y')

#writing to csv
combine.to_csv('combine.csv', encoding='utf-8', index=False)
这是我得到的输出:

Date,Tweet

07-Jul-2018,Ocean is still cold

12-Dec-2017,Done with school

09-Sep-2017,Friends will be friends

05-May-2017,Summer is about to begin

03-Mar-2016,Today is my final

01-Jan-2015,Happy New Year

07-Jul-2013,I want ice-cream!

06-Jun-2013,Hello World
显然,这一天是完全错误的,有人知道如何解决它吗?

你就不能简单地

df1['Date'] = pd.to_datetime(df1[['Year', 'Month', 'Day']])
df2['Month'] = df2.Month.apply(lambda x: datetime.strptime(x, '%B').month)
df2['Date'] = pd.to_datetime(df2[['Year', 'Month', 'Day']])

df = pd.concat([df1, df2])[['Date','Tweet']]
例如:

你不能简单地说吗

df1['Date'] = pd.to_datetime(df1[['Year', 'Month', 'Day']])
df2['Month'] = df2.Month.apply(lambda x: datetime.strptime(x, '%B').month)
df2['Date'] = pd.to_datetime(df2[['Year', 'Month', 'Day']])

df = pd.concat([df1, df2])[['Date','Tweet']]
例如:

  • df1
    s相关列上使用
    pd.to_datetime
  • 将年、月、日期作为字符串缝合在一起,然后传递到
    pd。对于
    df2
  • 使用
    pd.concat
    放置连接
  • 使用
    assign
    lambda
    以格式化字符串覆盖
    Date

  • df1
    s相关列上使用
    pd.to_datetime
  • 将年、月、日期作为字符串缝合在一起,然后传递到
    pd。对于
    df2
  • 使用
    pd.concat
    放置连接
  • 使用
    assign
    lambda
    以格式化字符串覆盖
    Date


如果你能提供一个方便测试的数字,那会有帮助。我注意到你在
df2
中有很长的格式月份,添加了一个数字转换。我不确定你的意思,我只想保留一年中的最后两位数字和正确的日期。正确的数据表已排序,您知道如何排序吗?
'January'
vs
1
-您应该有足够的以上内容来解决剩余的问题。如果您提供一个易于测试的表,这会有所帮助。我注意到您在
df2
中有长格式月份,添加了一个数字转换。我不确定您的意思,我只想保留一年中的最后两位数,并有正确的日期。正确的数据表已排序,您知道如何排序吗?
'January'
vs
1
-您应该有足够的以上内容来解决剩余问题。。无法将图片复制并粘贴到代码编辑器中。因此,要有人帮忙会困难得多,也就不太可能了。为了最大限度地利用网站,重要的是,包括创建一个。谢谢你,先生,我下次会记得这样做的。无法将图片复制并粘贴到代码编辑器中。因此,要有人帮忙会困难得多,也就不太可能了。为了最大限度地利用网站,重要的是,这包括创建一个。谢谢你,先生,我下次会记得这样做的
pd.concat([
    df1[['Tweet']].assign(Date=pd.to_datetime(df1.drop('Tweet', 1))),
    df2[['Tweet']].assign(Date=pd.to_datetime(
        [f'{y}-{m}-{d}' for _, m, d, y, *_ in df2.itertuples()]))
])[['Date', 'Tweet']].assign(Date=lambda d: d.Date.dt.strftime('%d-%b-%y'))

        Date                     Tweet
0  02-Jun-13               Hello World
1  23-Jul-13         I want ice-cream!
2  30-Sep-17   Friends will be friends
3  12-Dec-17          Done with school
0  02-Jan-15            Happy New Year
1  21-Mar-16         Today is my final
2  30-May-17  Summer is about to begin
3  15-Jul-18       Ocean is still cold