如何组合DataFame列数据和固定文本字符串

2024-05-18 17:42:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在一个更大的数据帧中组合4列和一个自定义(空格)分隔符(我已经用下面的代码完成了),但是我想在每个连接的开始和结束处添加一个固定的字符串。 这些列是X&Y坐标对,但是可以将它们作为str来处理(一旦我修剪到小数点后3位)。你知道吗

我发现在这个网站上有很多选择加入专栏,但没有一个加入专栏和一致的固定字符串。字符串懒惰的方法是我只需要再创建两个数据帧列,一个用于开始,一个用于结束,然后cat所有内容。有更复杂的方法吗?你知道吗

import pandas as pd
from pandas import DataFrame
import numpy as np

def str_join(df, sep, *cols):
    from functools import reduce
    return reduce (lambda x,y: x.astype(str).str.cat(y.astype(str), sep=sep),
                   [df[col] for col in cols])

data= pd.read_csv('/Users/XXXXXX/Desktop/Lines.csv')
df=pd.DataFrame(data, columns=['Name','SOLE','SOLN','EOLE','EOLN','EOLKP','Wind','Wave']) 

df['SOLE']=round(df['SOLE'],3)
df['SOLN']=round(df['SOLN'],3)
df['EOLE']=round(df['EOLE'],3)
df['EOLN']=round(df['EOLN'],3)

df['WKT']=str_join(df,' ','SOLE','SOLN','EOLE','EOLN')

df.to_csv('OutLine.csv') #turn on to create output file

这给了我很多。你知道吗

WKT
476912.131 6670122.285 470329.949 6676260.271

我要做的是将'(LINESTRING'添加到每个串联的开头,并将')'添加到每个串联的结尾,以给出我的答案。你知道吗

WKT
(LINESTRING 476912.131 6670122.285 470329.949 6676260.271 )

Tags: csv数据字符串importdfwktseppd
3条回答

您的功能已经很好了,只需添加以下几点:

def str_join(df, sep, *cols):
    # All cols must be numeric to use df[col].round(3)
    from functools import reduce
    return reduce (lambda x,y: 'LINESTRING ' + x.astype(str).str.cat(y.astype(str) + ' )', sep=sep),
                   [df[col].round(3) for col in cols])

还可以创建要导出的列的集合,快速设置数据类型格式,并应用联接。你知道吗

target_cols = ['SOLE','SOLN','EOLE','EOLN',]


# Make sure to use along axis 1 (columns) because default is 0
# Also, if you're on Python 3.6+, I think you can use f-strings to format your floats.
df['WKT'] = df[target_cols].apply(lambda x: '(LINESTRING ' + ' '.join(f"{i:.3f}" for i in x) + ')', axis=1)

结果:

In [0]: df.iloc[:,-3:]

Out [0]:
        Wind   Wave                                                WKT
    0  wind1  wave1  (LINESTRING 476912.131 6670122.285 470329.949 ...

**对不起,我使用的是Spyder,它是一个终端输出miser。这是“WKT”的打印件

In [1]: print(df['WKT'].values)
Out [1]: ['(LINESTRING 476912.131 6670122.285 470329.949 6676260.271)']

***编辑:要在“SOLN”后添加逗号,我们可以使用另一种方法:

target_cols = ['SOLE','SOLN','EOLE','EOLN',]

# Format strings in advance
# Set comma_col to our desired column name. This could also be a tuple for multiple names, then replace `==` with `in` in the loop below.

comma_col = 'SOLN'


# To find the last column, which doesn't need a space here, we just select the last value from our list.  I did it this way in case our list order doesn't match the dataframe order.

last_col = df[target_cols].columns.values.tolist()[-1]


# Traditional if-then method
for col in df[target_cols]:
    if col == comma_col:
        df[col] = df[col].apply(lambda x: f"{x:.3f}" + ",") # Explicit comma
    elif col == last_col:
        df[col] = df[col].apply(lambda x: f"{x:.3f}")
    else:
        df[col] = df[col].apply(lambda x: f"{x:.3f}" + " ") # Explicit whitespace

# Adding our 'WKT' column as before, but the .join() portion doesn't have a space in it now.
df['WKT'] = df[target_cols].apply(lambda x: '(LINESTRING ' + ''.join(i for i in x) + ')', axis=1)

最后:

In [0]: print(df['WKT'][0])
Out [0]: (LINESTRING 476912.131 6670122.286,470329.950 6676260.271)

这样用吧

df['new']='LINESTRING'
df['WKT']=pd.concat([df['new'],df['SOLE'],df['SOLN'],df['EOLE'],df['EOLN']])

相关问题 更多 >

    热门问题