社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Timus

Timus 最近创建的主题
Timus 最近回复了
3 年前
回复了 Timus 创建的主题 » 将Word中的日期转换为python中的历元时间戳

根据评论中的输入,您可以尝试:

from datetime import datetime

dates = {'Date': ['May 01, 2022', 'Apr 30, 2022', 'Apr 29, 2022', 'Apr 28, 2022', 'Apr 27, 2022']}
dates['Date'] = [
    datetime.strptime(date, "%b %d, %Y").timestamp() for date in dates['Date']
]
3 年前
回复了 Timus 创建的主题 » 如何使用自定义格式使用python组合2个文件

假设:输入文件看起来像

name/age/occupation
Bob/16/student

如果不关心输出中的填充,可以尝试:

with open('file_1.txt', 'r') as file1, open('file_2.txt', 'r') as file2:
    for line1, line2 in zip(
        file1.read().rstrip().split("/"), file2.read().rstrip().split("/")
    ):
        print(f"{line1} :- '{line2}'")

输出如下所示:

name :- 'Bob'
age :- '16'
occupation :- 'student'

如果要将其写入新文件:

with open('file_1.txt', 'r') as fin1, open('file_2.txt', 'r') as fin2,\
     open('file_3.txt', 'w') as fout:
    fout.writelines(
        f"{line1} :- '{line2}'\n"
        for line1, line2 in zip(
            fin1.read().rstrip().split("/"), fin2.read().rstrip().split("/")
        )
    )

如果你关心填充物,你可以尝试:

lines = []
for filename in ('file_1.txt', 'file_2.txt'):
    with open(filename, 'r') as file:
        lines.append(file.read().rstrip().split("/"))
padding = max(map(len, lines[0]))
for line1, line2 in zip(*lines):
    print(f"{line1.ljust(padding)} :- '{line2}'")

输出如下所示:

name       :- 'Bob'
age        :- '16'
occupation :- 'student'

在这里,您必须首先评估第一个文件并使用它 padding = max(map(len, lines[0])) 确定最大字符串长度,然后将其与 str.ljust() 相应地调整输出。

写入新文件:

...
with open('file_3.txt', 'w') as file:
    file.writelines(
        f"{line1.ljust(padding)} :- '{line2}'\n" for line1, line2 in zip(*lines)
    )

如果你有多个输入行,我会使用 csv 标准库中的模块,如

import csv

with open("file_1.txt", "r") as fin1, open("file_2.txt", "r") as fin2:
    reader1 = csv.reader(fin1, delimiter="/")
    reader2 = csv.reader(fin2, delimiter="/")
    for row1, row2 in zip(reader1, reader2):
        for item1, item2 in zip(row1, row2):
            print(f"{item1} :- '{item2}'")

或者用填充物

with open("file_1.txt", "r") as file:
    padding = max(
        len(item) for row in csv.reader(file, delimiter="/") for item in row
    )

with open("file_1.txt", "r") as fin1, open("file_2.txt", "r") as fin2:
    reader1 = csv.reader(fin1, delimiter="/")
    reader2 = csv.reader(fin2, delimiter="/")
    for row1, row2 in zip(reader1, reader2):
        for item1, item2 in zip(row1, row2):
            print(f"{item1.ljust(padding)} :- '{item2}'")
3 年前
回复了 Timus 创建的主题 » 如何在Python中以不同的大小水平重塑数据帧?

另一种选择,前提是您的数据帧

data = {
    'symbol': [1712, 1726, 1824, 1871, 1887, 1871, 1887, 1871, 1887],
    'weight': [0.007871, 0.00765, 0.032955, 0.006443, 0.00784, 0.006443, 0.00784, 0.006443, 0.00784],
    'lqdty': [7.023737, 3.221021, 3.475508, 4.615002, 6.678486, 4.615002, 6.678486, 4.615002, 6.678486],
    'date': [20210104, 20210104, 20210104, 20210105, 20210105, 20210105, 20210105, 20210106, 20210106]
}
index = [0, 1, 2, 0, 1, 2, 3, 0, 1]
df = pd.DataFrame(data, index=index)

会是

groups = pd.Series(df.index).eq(0).cumsum().values
result = pd.concat((sdf for _, sdf in df.groupby(groups)), axis=1)

结果:

   symbol    weight     lqdty        date  symbol    weight     lqdty  \
0  1712.0  0.007871  7.023737  20210104.0    1871  0.006443  4.615002   
1  1726.0  0.007650  3.221021  20210104.0    1887  0.007840  6.678486   
2  1824.0  0.032955  3.475508  20210104.0    1871  0.006443  4.615002   
3     NaN       NaN       NaN         NaN    1887  0.007840  6.678486   

       date  symbol    weight     lqdty        date  
0  20210105  1871.0  0.006443  4.615002  20210106.0  
1  20210105  1887.0  0.007840  6.678486  20210106.0  
2  20210105     NaN       NaN       NaN         NaN  
3  20210105     NaN       NaN       NaN         NaN