我在pandas DataFrame中的数据如下所示:
time
10:00am
11:00am
12:30pm
1:45pm
10:00pm
我想要的输出是:
time
10:00 am
11:00 am
12:30 pm
1:45 pm
10:00 pm
答案 0 :(得分:0)
您可以使用public class QuickSelect<T extends Comparable<T>> extends Sort<T> implements Selection<T> {
Class<T> t; // for class type
QuickSelect(Class<T> t){
this.t = t;
}
@Override
public T select(T[] data, int n, int k) {
if(data.length == 0) return null;
if(k == 1) return data[0];
if(k >= n || k <=0 ) return null;
Random randomGenerator = new Random();
int pivotPosition = randomGenerator.nextInt(n-1);
T pivotValue = data[pivotPosition];
ArrayList<T> lessThanPivot = new ArrayList<T>();
ArrayList<T> equalToPivot = new ArrayList<T>();
ArrayList<T> greatThanPivot = new ArrayList<T>();
for(int i=0; i < n; i++){
if(compare(pivotValue, data[i]) < 0) lessThanPivot.add(data[i]);
else if(compare(pivotValue, data[i]) == 0) equalToPivot.add(data[i]);
else greatThanPivot.add(data[i]);
}
Class<?> tClass = t.getClass();
if(k <= lessThanPivot.size()) select(lessThanPivot.toArray(), lessThanPivot.size(), k); // this part of the code is where the issue is
return null; //don't worry about this for now
}
}
:
str.replace
或者@Kartik建议,df['time'].str.replace(r'(am|pm)', r' \1')
可以做同样的事情:
Series.replace
或切片和连接:
df['time'].replace(to_replace=r'(am|pm)', value=r' \1', regex=True)
两者都产生新列:
df['time'].str[:-2] + ' ' + df['time'].str[-2:]
顺便说一句,如果你正在处理时间和/或日期,熊猫对datetime和timedelta类型有很好的支持。如果您正在进行分析,这些比字符串类型更容易使用。